NSMutableArray对象

时间:2010-04-11 14:31:54

标签: iphone objective-c ipad nsmutablearray

首先,我对Objective C和iPhone编程非常陌生。现在已经不在了。我已经阅读了大部分有关此文档和一些第三方手册的Apple文档。

我想我只是想知道我是否正确地采用这种方式......

- (NSMutableArray *)makeModel {

    NSString *api = @"http://www.mycoolnewssite.com/api/v1";

    NSArray *namesArray = [NSArray arrayWithObjects:@"News", @"Sports", @"Entertainment", @"Business", @"Features", nil];

    NSArray *urlsArray = [NSArray arrayWithObjects:
                      [NSString stringWithFormat:@"%@/news/news/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/sports/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/entertainment/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/business/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/features/25/stories.json", api], nil];

    NSMutableArray *result = [NSMutableArray array];

    for (int i = 0; i < [namesArray count]; i++) {
        NSMutableDictionary *objectDict = [NSMutableDictionary dictionary];
        NSString *name = (NSString *)[namesArray objectAtIndex:i];
        NSString *url = (NSString *)[urlsArray objectAtIndex:i];
        [objectDict setObject:name forKey:@"NAME"];
        [objectDict setObject:url forKey:@"URL"];
        [objectDict setObject:@"NO" forKey:@"HASSTORIES"];
        [result addObject:objectDict];
    }

    return result;
}

结果的输出是......

(
    {
    HASSTORIES = NO;
    NAME = News;
    URL = "http://www.mycoolnewssite.com/api/v1/news/news/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Sports;
    URL = "http://www.mycoolnewssite.com/api/v1/news/sports/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Entertainment;
    URL = "http://www.mycoolnewssite.com/api/v1/news/entertainment/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Business;
    URL = "http://www.mycoolnewssite.com/api/v1/news/business/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Features;
    URL = "http://www.mycoolnewssite.com/api/v1/news/features/25/stories.json";
}
)

任何见解都将受到赞赏; - )

1 个答案:

答案 0 :(得分:4)

看起来很好。如果你关心,可以有一些小的改进。

<强> 1

[NSString stringWithFormat:@"%@/news/news/25/stories.json", api]

可以替换为

[api stringByAppendingString:@"/news/news/25/stories.json"]

如果api无法出现在中间或接受其他参数。

<强> 2

    NSString *name = (NSString *)[namesArray objectAtIndex:i];
    NSString *url = (NSString *)[urlsArray objectAtIndex:i];

显式演员表是不必要的。 id可以隐式地与其他ObjC对象进行转换。

第3

您可以使用方便的方法-dictionaryWithObjectsAndKeys:一次性构建字典,因此您不需要一个字典词典:

[result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
   name, @"NAME",
   url, @"URL",
   @"NO", @"HASSTORIES", nil]];

<强> 4。 (可选)

如果函数不是热点,则此转换无用。

由于数组仅在本地使用,因此使用C数组效率更高。

static const int arraySize = 5;
NSString* namesCArray[] = {@"News", @"Sports", @"Entertainment", @"Business", @"Features"};
NSString* urlsCArray[arraySize];
urlsArray[0] = [api stringByAppendingString:@"/news/news/25/stories.json"];
...
for (int i = 0; i < arraySize; ++ i) {
  ...
  NSString* name = namesCArray[i];
  NSString* url = urlsCArray[i];
  ...
}

这会删除重复的-count-objectAtIndex:调用,这与直接元素访问相比非常慢。

<强> 5。 (可选)

如果数组很短,则此变换无效。

您可以使用快速枚举来遍历ObjC容器:

int i = 0;
for (NSString* name in namesArray) {
  NSString* url = [urlsArray objectAtIndex:i];
  ...
  ++ i;
}

<强> 6

通常我们使用[NSNumber numberWithBool:NO]来表示盒装的true / false值,而不是字符串@"NO"。每当原始数字(intfloat等)不能被使用时(例如存储在NSArray中),NSNumber也被大量使用。我不知道您的API是否明确要求字符串为NO,因此它可能不适合您。