Jul*_*les 20 xcode cocoa-touch objective-c ios
在Xcode 7中,我收到了这个警告:
Null passed to a callee that requires a non-null argument
Run Code Online (Sandbox Code Playgroud)
..来自NSMutableArray的这个nil初始化...
sectionTitles = [[NSMutableArray alloc] initWithObjects:nil];
Run Code Online (Sandbox Code Playgroud)
我发现我应该使用它removeAllObjects.
[sectionTitles removeAllObjects];
Run Code Online (Sandbox Code Playgroud)
但是,这不允许我评估一个sectionTitles.count == 0.我确实尝试过sectionTitles == nil,但除非我使用iniWithObjects我以后不能添加对象.
我需要将数组设置为nil或0,当我刷新数据源时,当没有记录时.addObject除非我使用过,否则我似乎无法使用添加项目initWithObjects.
Swi*_*ect 19
传递非空参数只是部分答案.
新的Objective-C可空性注释对于围栏的Swift端的代码有很大的好处,但即使没有编写一行Swift,这里也有很大的好处.标记为的指针
nonnull现在将在自动完成期间给出提示,如果发送nil而不是正确的指针则会发出警告.
阅读NSHipster 综合文章.
为了在您自己的代码中利用相同的合同,请使用nonnull或nullable:
OBJ-C
- (nullable Photo *)photoForLocation:(nonnull Location *)location
Run Code Online (Sandbox Code Playgroud)
Jef*_*mas 17
你为什么不试试:
sectionTitles = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)
或以下任何一项:
sectionTitles = [[NSMutableArray alloc] initWithCapacity:sectionTitles.count];
sectionTitles = [NSMutableArray new];
sectionTitles = [NSMutableArray array];
sectionTitles = [NSMutableArray arrayWithCapacity:sectionTitles.count];
Run Code Online (Sandbox Code Playgroud)
也许是一些愚蠢的:
sectionTitles = [NSMutableArray arrayWithArray:@[]];
sectionTitles = [@[] mutableCopy];
Run Code Online (Sandbox Code Playgroud)
有很多方法可以创建空的可变数组.只需阅读文档.
| 归档时间: |
|
| 查看次数: |
29757 次 |
| 最近记录: |