碼代碼時碰到的一些小問題 & Xcode的一些使用方法


我還是菜鳥,寫下的記錄不一定完全正確。多找資料,多google吧。

Xcode 4.3.2

1、添加Frameworks,或者看圖

2、找不到設置,可以在搜索欄里搜索關鍵字。

比如設置自動引用計數(ARC),在右上方輸入關鍵字“ARC”。

3、添加輸出口(Outlets)或響應事件的方法(Actions),可以直接控件拖到對應的視圖控制器實例類文件中。

4、在Storyboard中能否使用xib文件?

A:見6。

5、用Storyboard建的視圖能否用在iOS4或更早的版本中?

A: storyboards will run only on devices sporting iOS 5 and above.

6、When to use Storyboard and when to use XIBs

7、Property:strong,weak?

strong & weak
1 strong
2 Specifies that there is a strong (owning) relationship to the destination object.
3 weak
4 Specifies that there is a weak (non-owning) relationship to the destination object.
5 If the destination object is deallocated, the property value is automatically set to nil.
6 (Weak properties are not supported on OS X v10.6 and iOS 4; use assign instead.)

 

8、To ARC or not to ARC? What are the pros and cons?

ARC is supported in Xcode 4.2 for Mac OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in Mac OS X v10.6 and iOS 4.

WWDC2011視頻之Introduction Automatic Reference Counting筆記

9、關於self.object 和 object 的區別, another

eg.

View Code
 1 // .h
 2   @property (retain, nonatomic) UILabel *label;
 3   //.....
 4   // .m
 5   @synthesize label;
 6   //.....
 7   
 8   - (void)viewDidUnload
 9   {
10       [super viewDidUnload];
11       // Release any retained subviews of the main view.
12       self.label = nil;
13   }
14   
15   - (void)dealloc
16   {
17       [label release];
18       [super dealloc];
19   }

 

10、使用ARC時,IBOutlets 指定屬性 strong 還是 weak

11、NSInteger等非對象的屬性(attribute)

12、給static UITableView cell 設置數據

13、Difference between Modal and Push segue in storyboards

14、靜態表

15、property 'tableView' 'retain(or strong)' attribute dose not match property inherited from 'UITableViewController'

1 // 之前
2 @property (weak, nonatomic) IBOutlet UITableView *tableView;

 

1 // 在UITableViewController.h 中
2 @property(nonatomic,retain) UITableView *tableView;

 

1 // 之后
2 @property (strong, nonatomic) IBOutlet UITableView *tableView;

 

參考鏈接

16、在視圖類B 中聲明協議,並在視圖類A中實現該協議的方法。在B中定義id <XXProtocol> delegate。在B中調用delegate的方法時,實際上沒有調用。

A:調試發現該delegate實例變量為空。最終確定為沒有給該delegate設置引用對象。

protocol
1 // Class B.h
2 
3 @protocol (weak, nonatomic) id <XXProtocol> delegate;
4 
5 
6 // Class A.m 
7 // 合適的地方
8 CB.delegate = self;

17、自定義NSdate對象的值,比如yy-MM-dd、hh:mm等,用plist存儲,再取出來時報錯:

error
1 2012-08-23 21:43:34.333 Course Table[1198:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isEqualToDate:]: unrecognized selector sent to instance 0x6bcc660'

經過很久debug發現,在存儲時進行了不必要的格式化:

View Code
 1 - (BOOL)createCTSettingPList
 2 {
 3     //...
 4     NSMutableDictionary *dayTime = [[NSMutableDictionary alloc] initWithCapacity:3];
 5     [dayTime setValue:[self.timeFormat stringFromDate:startTime] forKey:kCTStartTime];
 6     //...
 7     if ([plist writeToFile:[self getSettingFilePath] atomically:YES])
 8         return YES;
 9     return NO;
10 }

 

這樣做導致的后果:在取回這些對象后,他們包含的內容是不完整的(姑且這么理解)。

應該寫成這樣:

View Code
 1 - (BOOL)createCTSettingPList
 2 {
 3     //...
 4     NSMutableDictionary *dayTime = [[NSMutableDictionary alloc] initWithCapacity:3];
 5     [dayTime setValue:defaultTerm.startTime forKey:kCTStartTime];
 6     //...
 7     if ([plist writeToFile:[self getSettingFilePath] atomically:YES])
 8         return YES;
 9     return NO;
10 }

 

18、設計界面的時候,用到標簽欄,結合表視圖,這么打算:選中一行,在接下來的視圖中不顯示標簽欄。今天突然發現有這一選項“Hides Bottom Bar on Push”可以實現這個目的。“Is Initial View Controller”是把當前的視圖控制器設置為storyboard的入場控制器。

19、讓View Controller嵌套在Navigation View Controller中:

20、Do I need to release NSString generated using @“…”?

NSString *someString = @"somestring";
someString 是一個編譯時的常量字符串對象,不需要釋放。

創建對象的方法中包含 alloc、retain、copy 或者 new,則需要釋放該對象。

21、@interface ClassName(XXX)

1 @interface ClassName ( XXX )
2 // method declarations
3 @end

Categories,《The Objective-C Programming Language》

Informal Protocols,《The Objective-C Programming Language》

相關討論

22、子類是否繼承父類所遵循的協議?

A class is said to conform to a formal protocol if it adopts the protocol or inherits from another class that adopts it. An instance of a class is said to conform to the same set of protocols its class conforms to.

Conforming to a Protocol,《The Objective-C Programming Language》

23、How to initialize a custom prototype style table cell in iOS 5 storyboards?

Using Xcode Storyboards to Build Dynamic TableViews with Prototype Table View Cells

24、how to update data in tableView?

25、How to get all indexPaths from a UITableView?

26、在Core Data中怎么存儲數字?

用NSNumber。

27、報錯:wait_fences: failed to receive reply: 10004003

代碼中用到,UIAlertView,UITextField。

可能與UIAlertView和沒有隱藏鍵盤有關。

通過以下方法,就沒再報錯並產生停頓感:

View Code
 1 #pragma mark - UIAlertViewDelegate
 2 - (void)willPresentAlertView:(UIAlertView *)alertView
 3 {
 4     [self.textField resignFirstResponder];
 5 }
 6 
 7 // 某處
 8 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" 
 9                                                 message:@"不能為空" 
10                                                delegate:self
11                                       cancelButtonTitle:@"OK" 
12                                       otherButtonTitles:nil];
13 [alert show];

 28、@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

在- (NSFetchedResultsController *)fetchedResultsController中調用:

NSLog(@"%i", _fetchedResultsController.fetchedObjects.count);

第一次:0;

第二次:!=0;(在其他方法中調用)。

如果:

1 [_fetchedResultsController performFetch:&error]; // add this line
2 NSLog(@"%i", _fetchedResultsController.fetchedObjects.count);

第一次:!=0;

第二次:!=0;(在其他方法中調用)。

猜測,剛初始化后沒有執行 performFetch 操作。

29、為什么需要在Core Data 中的 Entities 之間的 Relationship 指定 Inverse?

Core Data uses bidirectional relationship information to maintain the consistency of the object graph (hence the Consistency Error) and manage undo and redo information. If you leave a relationship without an inverse, you imply that you will take care of the object graph consistency and undo/redo management. The Apple documentation strongly discourages this, however, especially in the case of a tomany relationship. When you don’t specify an inverse relationship, the managed object at the other end of the relationship isn’t marked as changed when the managed object at this end of the relationship changes.(摘自《Pro.Core.Data.for.iOS. Second.Edition》

30、是否需要實現 NSMangedobject 子類中的比如 add<Key>Object 和 remove<Key>s 方法?

描述:

1 @interface Employee (DirectReportsAccessors)
2 - (void)addDirectReportsObject:(Employee *)value;
3 - (void)removeDirectReportsObject:(Employee *)value;
4 - (void)addDirectReports:(NSSet *)value;
5 - (void)removeDirectReports:(NSSet *)value;
6 @end

不需要。

Managed Object Accessor Methods,《Core Data Programming Guide》。

There should typically be no need for you to provide your own implementation of these methods, unless you want to support scalar values. The methods that Core Data generates at runtime are more efficient than those you can implement yourself.

31、《Core Data’s default date value》

默認值可以使用帶雙引號的詞,形式如“now”,但是時間在編譯時決定,而不是運行時。

32、Core Data:在使用NSFetchedResultsController的情況下,在self.managedObjectContext保存后,沒有保存在到持久性存儲中?

應該這樣:

 1 NSError *error;
 2 NSManagedObjectContext *addingManagedObjectContext = controller.managedObjectContext;
 3         
 4 if (![addingManagedObjectContext save:&error]) 
 5 {
 6     NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
 7     abort();
 8 }
 9 if (![[self.fetchedResultsController managedObjectContext] save:&error]) 
10 {
11     NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
12     abort();
13 }

 

原因:

1 The new managedObject is associated with the add controller's managed object context.
2 This means that any edits that are made don't affect the application's main managed object context -- it's a way of keeping disjoint edits in a separate scratchpad. Saving changes to that context, though, only push changes to the fetched results controller's context. To save the changes to the persistent store, you have to save the fetch results controller's context as well.

摘自Apple‘s SampleCode。

33、error occurred when I execute “self.course.referenceBooks.count”

描述見鏈接。

原因:由於手動建立的model file,關系名(books)和Course類中的屬性不一致導致。

34、Creating Source Code to Implement a Custom Managed Object Class

創建源代碼用於實現自定義的 Managed Object 類。

35、在創建源代碼用於實現自定義的 Managed Object 類的時候,有着一個選項:use scalar properties for primitive data types

使用 scalar properties ,Core Data 不會動態生成 accessor methods,即需要自己手動實現。

鏈接

文檔鏈接

36、Insert a Table Header View (tableHeaderView) in StoryBoard

拖動控件至導航欄與表視圖之間。類似的界面可通過在ViewController中添加視圖控件和表示圖控件,但效果沒之間插入表頭視圖好。

 37、在使用UITableView時,當數據發生改變后,最好執行 [self.tableView reloadData];;其他,見4

 38、隱藏self.navigationItem.backBarButtonItem

self.navigationItem.hidesBackButton = NO;

 Xcode 4.5.2

39、在UIScrollView中添加圖片,在代碼中能實現預期的效果,而在SB中好UIScrollView則會使它的frame變形,它的高度比圖片的高度高很多時才不報錯。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM