因此遵循 Apple 的 DocInteraction 示例 here我放了一个 static NSString
来定义一些预加载的文档。现在我需要将它们加载到 viewDidLoad
方法中的 NSArray 中。如果我将它放入 for()
语句中,它会显示“表达式结果未使用”,而且我知道我可以使用 documents[indexPath.row]
解决它,但是这是 viewDidLoad 方法。那么我如何为每个静态 NSString 加载到 NSArray 中呢?
代码(编辑):
//Before @implementation
static NSString* documents[] =
{ @"Musette.pdf",
@"Minore.pdf",
@"Cantata.pdf",
@"Finalé.pdf"
};
//In viewDidLoad
if ( _icons == nil )
{
_icons = [[NSMutableArray alloc] init];
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 200.0, 324.0)
cornerRadius: 18.0];
for (NSUInteger i = 1; i <= 4; i++){
UIGraphicsBeginImageContext( CGSizeMake(200.0, 324.0) );
// clear background
[[UIColor clearColor] set];
UIRectFill( CGRectMake(0.0, 0.0, 200.0, 324.0) );
// fill the rounded rectangle
[path fill];
UIImage *image = [UIImage imageNamed:@"ClearImage.png"];
UIGraphicsEndImageContext();
// put the image into our list
[_icons addObject: image];
}
}
[_gridView reloadData];
最佳答案
Objective-C 中 for 语句的 for-each 修改不支持 C 数组。所以你在这里有 2 个选择:
使用普通的for语句循环
for(int idx = 0; idx < KNOWN_DOCUMENTS_COUNT;++idx) { ...
创建 NSEnumerator 的自定义子类并使用它...但是,我相信,对于这种情况,这将是一个很大的开销
关于objective-c - 分离静态 NSString 以加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983105/