use*_*382 3 iphone xcode objective-c ios
所以我一直在Xcode4中为一个有多个集合视图的应用程序工作(准确地说是4)
我有两个视图,一个底部的"主"视图,其中包含一个填充屏幕的大型集合视图,以及一个较小的"抽屉"视图,可以从侧面拉出并包含三个窄的,水平滚动的集合视图,一个堆叠在一起另一个的顶部.
我一直使用标签和cellforitematpath方法中的if语句来填充单元格.在xcode 4中,以及我在xcode 4中创建的项目,我现在在xcode 5中打开,这是有效的.我在底部得到一个警告(而不是错误)control may reach end of non void function,但我仍然可以构建和运行它的工作原理.
在xcode 5中的一个新项目中,相同的代码将"控制可能达到非空函数的结尾"视为错误,并且不允许构建和运行.这无穷无尽令人沮丧.除非有一个数组要填充单元格,否则我不想返回单元格.我甚至都不知道'返回零'是什么意思.有没有人有任何关于如何关闭它的建议?
以下绝对不是很好,请自担风险:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag == 0){
static NSString *CellIdentifier = @"subjectCell";
SubjectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
AHSubject *cellSubject = [self.subjectsArray objectAtIndex:indexPath.row];
[[cell subjectLabel]setText:cellSubject.name];
return cell;
}
else if (collectionView.tag == 1){
if ([categoryArray count] > 0) {
static NSString *CellIdentifier = @"categoryCell";
CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
AHCategory *cellCategory = [categoryArray objectAtIndex:indexPath.row];
[[cell categoryLabel]setText:cellCategory.name];
return cell;
}
}
else if (collectionView.tag == 2){
if ([subcategoryArray count] > 0) {
static NSString *CellIdentifier = @"subcategoryCell";
GroupCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
AHCardGroup *cellSubcategory = [subcategoryArray objectAtIndex:indexPath.row];
[[cell subcategoryLabel]setText:cellSubcategory.name];
return cell;
}
}
else {
if ([cardArray count] > 0) {
static NSString *CellIdentifier = @"cardCell";
CardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
AHCard *cellCard = [cardArray objectAtIndex:indexPath.row];
[[cell frontLabel]setText:cellCard.front];
return cell;
}
}
// return nil;
}
Run Code Online (Sandbox Code Playgroud)
只是取消注释return nil,它会没事的.因为你的最终else处理所有情况,代码永远不会达到return nil,但Xcode的分析器显然不够聪明,不知道这一点.
编辑:此设置可在Apple LLVM 5.0 - 警告 - 所有语言下的构建设置中找到.它属于"不匹配的返回类型",以前默认为"是",现在默认为"是"(视为错误).您可以更改此设置以将其更改回警告.但我不会这样做.只是取消注释return nil,这很好.
我会指出,在没有返回值的情况下到达非void函数的末尾是一个错误,并且很可能导致崩溃,而返回nil(例如)可能不会.考虑以下简单的例子:
- (NSString*) someString
{
return nil;
}
// ... someplace elsewhere in the code:
NSString* aString = [self someString];
NSLog( @"aString = %@", aString );
Run Code Online (Sandbox Code Playgroud)
这很好,你的输出将是这样的:
aThing返回(null)
现在,如果你注释掉那个回报,那么:
- (NSString*) someString
{
// return nil;
}
// ... someplace elsewhere in the code:
NSString* aString = [self someString];
NSLog( @"aString = %@", aString );
Run Code Online (Sandbox Code Playgroud)
NSLog调用将取消引用一些随机内存位置(无论发生在堆栈上,我认为),你的应用程序几乎肯定会崩溃.
如果您有一个返回某种类型值的函数,它必须始终返回该类型的值.
EDIT2(响应@戴夫德隆的评论):好点,返回nil的collectionView:cellForItemAtIndexPath: 是一个错误,但我可能会认为这是一个令人震惊的少错误不是简单地让没有返回值的函数结束.事实证明这是一种学术上的,因为OP的代码return nil无论如何都会永远不会执行.
可以用这样的方式重写函数,以避免警告并输入"return nil",因此:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
id cell = nil;
if (collectionView.tag == 0) {
// Make a Subject cell
}
else if (collectionView.tag == 1) {
// Make a Category cell.
}
else {
// Make a Card cell.
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
...但最终结果与OP的原始代码相同(未return nil注释):该函数可以但不会返回nil.
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |