Những nội dung chính của trang này:
1. Những loại file cần biết trong Objective-C:
Extension | Meaning |
---|---|
.c | C language source file |
.cc, .cpp | C++ language source file |
.h | Header file |
.m | Objective-C source file |
.mm | Objective-C++ source file |
.pl | Perl source file |
.o | Object (compiled) file |
2. Những đối tượng collection:
- Arrays: là loại collection lưu trữ dữ liệu được sắp xếp theo thứ tự. Có 2 kiểu đối tượng như: NSArray (không cho edit) và NSMutableArray (là lớp con của NSArray, cho edit).
+ Điểm mạnh: Tìm kiếm nhanh theo vị trí (index),
+ Điểm yếu: Chậm khi tìm kiếm theo giá trị (value), chậm khi insert/delete.
- Dictionaries: là loại collection lưu trữ dữ liệu được sắp xếp theo key-value. Có 2 kiểu đối tượng như: NSDictionary (không cho edit) và NSMutableDictionary (là lớp con của NSDictionary, cho edit).
+ Điểm mạnh: Được lưu trữ theo cặp key-value, vì thế có thể tìm kiếm nhanh theo key.
+ Điểm yếu: Chưa biết.
- Sets: là loại collection lưu trữ đối tượng không theo thứ tự, có hỗ trợ việc kiểm tra đối tượng bên trong bị lặp lại (distinct objectsobjects). Có 3 kiểu đối tượng như: NSSet (không cho edit), NSMutableSet (là lớp con của NSSet, cho edit) và NSCountedSet (là lớp con của NSMutableSet, kiểu non-distinct objects). Giả sử như bạn muốn lưu danh sách những nước mà bạn đã từng đi du lịch, 1 nước có thể bạn sẽ đi nhiều lần khi insert vào kiểu dữ liệu là Sets thì nó sẽ tự động lọc cho bạn, vì thế bạn có thể truy suất nhanh được.
+ Điểm mạnh: Tìm kiếm nhanh theo giá trị, nhanh khi insert/delete.
+ Điểm yếu: Chưa biết.
Code ví dụ đơn giản về Sets:
NSSet* aSet = [[NSSet alloc] initWithArray:@[@"a",@"b",@"a",@"c"]];
NSLog(@"Count of NSSet:%ld",aSet.count);
NSMutableSet* mSet = [[NSMutableSet alloc] initWithArray:@[@"a",@"b",@"a",@"c"]];
NSLog(@"Count of NSMutableSet:%ld",mSet.count);
NSCountedSet* cSet = [[NSCountedSet alloc] initWithArray:@[@"a",@"b",@"a",@"c"]];
NSLog(@"Count of NSCountedSet:%ld",cSet.count);
NSLog(@"Count of 'a' value:%ld",[cSet countForObject:@"a"]);
NSLog(@"Count of 'b' value:%ld",[cSet countForObject:@"b"]);
Kết quả in trên màn hình Console như sau:Count of NSSet:3
Count of NSMutableSet:3
Count of NSCountedSet:3
Count of 'a' value:2
Count of 'b' value:1
+ Bạn có thể xem chi tiết trong blog hướng dẫn sử dụng kiểu dữ liệu Sets.- IndexSets:(đối tượng như NSIndexSet, NSMutableIndexSet, NSRange) là những đối tượng bổ trợ, giúp khả năng mở rộng sức chứa của một kiểu dữ liệu khác, ví dụ như array. Hình minh hoạ sử dụng IndexSet là NSRange để truy suất những giá trị tại 1 ví trí index hay 1 khoảng vị trí theo index trong array:
Giả sử mình có code muốn lấy giá trị tại vị trí như 1,3,4,8 của 1 mảng, thì cách làm như sau:
NSArray* list = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
NSIndexSet* indexSet8 = [[NSIndexSet alloc] initWithIndex:8];
NSMutableIndexSet* mIndexSet = [[NSMutableIndexSet alloc] init];
[mIndexSet addIndex:1];
[mIndexSet addIndexesInRange:NSMakeRange(3, 2)];
[mIndexSet addIndexes:indexSet8];
NSArray* filterList = [list objectsAtIndexes:mIndexSet];
NSLog(@"%@",filterList);
Kết quả in trên màn hình Console như sau:(
1,
3,
4,
8
)
- IndexPath: dùng để lưu trữ vị trí của thông tin trong kiến trúc collection phức tạp. Các bạn xử lý trên table hay gặp đối tượng này. Sơ đồ ví dụ về đối tượng này như sau:
Hay trong kiến trúc dạng cây như sau:
- Copying Collection: Dùng để copy những đối tượng sang vùng nhớ mới hoặc có thể dùng chung vùng nhớ cũ.
+ Shallow copy: là kiểu copy thông thường, cùng đối tượng trong collection cùng trỏ đến 1 vùng nhớ. Vì thế nếu mình huỷ vùng nhớ của những đối tượng trong Array1 đi thì những đối tượng đó trong Array2 cũng bị ảnh hưởng theo. Sử dụng bằng cách gọi hàm "initWithArray:copyItems:" với thuộc tính copyItems là NO, copyWithZone:nil, hoặc bằng cách gán thông thường.
+ Deep copy: là kiểu copy phát sinh thêm vùng nhớ mới. Vì thế nếu mình huỷ vùng nhớ của nhứng đối tượng trong Array1 đi thì những đối tượng trong Array2 vẫn còn sử dụng bình thường. Sử dụng bằng cách gọi hàm "initWithArray:copyItems:" với thuộc tính copyItems là YES.
- Tất cả những collection tham khảo từ mục số 7 trong blog của Raywenderlich.
3. Những macro cần thiết:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef DEBUG | |
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); | |
#else | |
# define DLog(...) | |
#endif | |
// ALog will always output like NSLog | |
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) | |
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) | |
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0) | |
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) | |
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) | |
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) | |
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) | |
#define IS_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) | |
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) | |
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) | |
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) | |
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) |
4. Cách sử dụng #ifdef:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef __IPHONE_9_0 | |
// Hide panel in IOS9 | |
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) { | |
self.inputAssistantItem.leadingBarButtonGroups = @[]; | |
self.inputAssistantItem.trailingBarButtonGroups = @[]; | |
} | |
#endif |
5. Tắt warning trên XCode:
- Bạn muốn tắt cảnh báo này trên xcode thì viết đoạn code như sau:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma GCC diagnostic push | |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
if (![self respondsToSelector:@selector(edgesForExtendedLayout)]) { | |
self.wantsFullScreenLayout = YES; | |
} | |
#pragma GCC diagnostic pop |
6. Những chủ đề mở rộng trong Objective C:
- Method Swizzling
7. Canh chỉnh table view khi keyboard hiển thị:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Adjust table when keyboard is displayed. | |
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) { | |
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; | |
CGRect _keyboardFrame = CGRectNull; | |
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame]; | |
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ | |
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)]; | |
} completion:nil]; | |
}]; | |
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note) { | |
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ | |
[_tableView setContentInset:UIEdgeInsetsZero]; | |
} completion:nil]; | |
}]; |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.