Thứ Tư, 26 tháng 6, 2013

LẬP TRÌNH OBJECTIVE-C - part 2

2. Các cấu trúc điều khiển

-          Các phép toán thường dùng
Phép toán so sánh
Phép toán
Mô tả
==
bằng
!=
Không bằng
> 
Lớn hơn
< 
Nhỏ hơn
>=
Lớn hơn hoặc bằng
<=
Nhỏ hơn hoặc bằng


Phép toán logic
Phép toán
Mô tả
!
khác
&&
||
Hoặc

-          If/else
if (condition) {
         //statement(s) if the condition is true;
}
else {
//statement(s) if the condition is not true;
  }

-          For
for (counter; condition; update counter) {
         //statement(s) to execute while the condition is true;
}

-          For In
for (Type newVariable in expression ) {
         //statement(s);
}
// or
//Type existingVariable ;
for (existingVariable in expression) {
//statement(s);
}
-          Sdụng khi
Ø  NSFastEnumeration
Ø  NSArray, NSSet
Ø  NSDictionary
Ø  NSManagedObjectModel

-          While
while (condition) {
         //statement(s) to execute while the condition is true
}

-          Do While
do {
         //statement(s) to execute while the condition is true
} while (condition);

-          Jump statement
Ø  Return; dừng thực hiện và trả về hàm gọi đến hàm dừng thực hiện này
Ø  Break; dừng vòng lặp
Ø  Continue; bỏ qua phần còn lại và bắt đầu lại từ đầu trong vòng lặp
Ø  Goto

Ø  Exit(); thoát khỏi chương trình

3. Array

Tạo mảng:
-          NSArray
NSArray *myColors;
myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
-          NSMutableArray
NSMutableArray *myColors;
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
-          Xử dụng arrayWithObject để tạo ra các đối  tượng item, ở đây tôi tạo mới 1 đối tượng mảng với 4 đuối tượng con kiểu String là red, green, blue, yellow. Và nil nằm cuối cùng, nếu không có nil nằm cuối cùng sẽ gây ra lỗi.
-          Khi khỏi tạo thi NSArray tạo ra mảng và không thể thay đổi, còn NSMutableArray thì ngược lại tức là cho phép thay đổi nội dung của mảng.

Tính số lượng đối tượng con trong mảng
NSLog (@"Number of elements in array = %lu", [myColors count]);

Xuất ra
Number of elements in array = 4

Xử lý các phần tử trong mảng
NSArray *myColors;
int i;
int count;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
count = [myColors count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
Xuất ra
Element 0 = Red
Element 1 = Green
Element 2 = Blue
Element 3 = Yellow

Duyệt mảng nhanh
NSArray *myColors;
NSString *color;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
for (color in myColors)
NSLog (@"Element = %@", color);

Xuất ra
Element 0 = Red
Element 1 = Green
Element 2 = Blue
Element 3 = Yellow
Thêm phần tử mới vào mảng
NSMutableArray *myColors;

myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors addObject: @"Indigo"];
[myColors addObject: @"Violet"];

-          Chèn phần tử vào mảng với số thứ tự
NSMutableArray *myColors;
int i;
int count;

myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors insertObject: @"Indigo" atIndex: 1];
[myColors insertObject: @"Violet" atIndex: 3];

count = [myColors count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);

Xuất ra
Element 0 = Red
Element 1 = Indigo
Element 2 = Green
Element 3 = Violet
Element 4 = Blue
Element 5 = Yellow

Sắp xếp các phần tử trong mảng
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil];
NSArray *sortedArray;
sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

-          Mảng được sắp xếp theo hàm localizedCaseInsensitiveCompare, ngoài ra còn có các hàm sắp xếp khác như NSOrderedAscending, NSOrderedSame or NSOrderedDescending

Xóa phần tử từ mảng
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

-          Xóa theo vị trị số thứ tự
[myColors removeObjectAtIndex: 0];

-          Xóa phần tử theo giá trị của phần tử cụ thể
[myColors removeObject: @"Red"];

-          Xóa tất cả phần tử trong mảng theo giá trị cụ thể của phần tử cần xóa
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", @"Red", @"Red", nil];

[myColors removeObjectIdenticalTo: @"Red"];

-          Xóa tất cả các phần tử trong mảng
[myColors removeAllObjects];

-          Xóa phần tử cuối cùng trong mảng
[myColors removeLastObject];
tài liệu tham khảo

Không có nhận xét nào:

Đăng nhận xét