【问题标题】:Why can I fill my NSArray just with one object?为什么我可以只用一个对象填充我的 NSArray?
【发布时间】:2010-01-20 00:18:55
【问题描述】:

为什么我不能填充我的 NSArray?我的错误在哪里?他总是只将一个对象/图像填充到 nsarray。我设置了一个 NSlog 来检查哪个值字符串具有,他向我显示了所有 20 个 url。

for (int i = 0, count = [bild count]; i < count; i = i++) {
    NSString * string = [bild objectAtIndex:i];
    NSURL *url = [NSURL URLWithString:string];
    NSData *datas = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:datas];
    myArray = [NSMutableArray array];      // this will autorelease, so if you need to keep it around, retain it
    [myArray addObject:img];
    int count = [myArray count];
    NSLog(@"There are %d elements in my array", count);
}

感谢您的帮助!

【问题讨论】:

  • 你把编译器称为他?
  • 如果不是简单的错字,德语名词是有性的。

标签: iphone objective-c xcode nsarray


【解决方案1】:

每次循环都在创建一个新数组

 myArray = [NSMutableArray array];  

【讨论】:

    【解决方案2】:

    每次循环,您都在重新初始化您的数组。将数组声明移出循环(在它之前)以解决此问题。

    【讨论】:

      【解决方案3】:
      1. 在循环外创建 NSMutable 数组
      2. 在 NSMutableArray 上调用 addObject

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多