码代码时碰到的一些小问题 & 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变形,它的高度比图片的高度高很多时才不报错。

 

posted @ 2012-09-14 15:04  SubmarineX  阅读(4336)  评论(0编辑  收藏  举报