objective-c - 分离静态 NSString 以加载到数组中

标签 objective-c ios ipad static nsstring

因此遵循 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 个选择:

  1. 使用普通的for语句循环

    for(int idx = 0; idx < KNOWN_DOCUMENTS_COUNT;++idx) { ...

  2. 创建 NSEnumerator 的自定义子类并使用它...但是,我相信,对于这种情况,这将是一个很大的开销

关于objective-c - 分离静态 NSString 以加载到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983105/

相关文章:

ios - 在主包中搜索 XIB

iOS 应用程序仅在 iPad Air 上崩溃

ios - 在 Xamarin.iOS DYLD 中集成时,代码签名对于 iOS Objective C Umbrella 框架无效,库未加载 : App Crash

ios - 使用适用于 iOS v2 的 Google Analytics SDK 跟踪屏幕层次结构

iphone - 委托(delegate)被派往错误的类(class)

ios - 我们如何使用 KeychainItemWrapper 类(ARC)保存 NSArray

html - 尝试从 html 中提取表数据

objective-c - 检测绘制图像的百分比是 "erased"

ios - 如何在 UIPageViewController 上弹出 View ?

ios - UIView 部分离开屏幕时出现 OpenGL ES 性能问题