3

我正在尝试完成斯坦福 iPhone 编程(FA10)作业“Flickr Fetcher”——到目前为止一切进展顺利,但我陷入了僵局:

我已成功提取出“Top 100”图片的位置,这些图片的格式为“Country, State, City”。我想创建两个 NSString——一个是国家,另一个是州和城市。然后我可以从哪里做

cell.textLabel.text = countryString;
cell.detailTextLabel.text = stateCityString;

在我的表视图数据源方法中。

根据 stackoverflow 和Apple Documentaion的研究 NSScanner 似乎是我最好的选择——这就是我目前所拥有的……

- (void)viewDidLoad {
    //Get the top 100 photos from Flickr
    self.topPlacesArray = [FlickrFetcher topPlaces];

    NSString *mainLabelString = [[NSString alloc] init];
    NSString *stringFromArray = [[NSString alloc] init];

    //This retrieves the string of the location of each photo
    stringFromArray = [topPlacesArray valueForKey:@"_content"];

    NSScanner *theScanner = [NSScanner scannerWithString:stringFromArray];
    NSCharacterSet *commaSet = [[NSCharacterSet alloc] init];
    commaSet = [NSCharacterSet characterSetWithCharactersInString:@","];

    while ([theScanner isAtEnd] == NO) {
        if ([theScanner scanUpToCharactersFromSet:commaSet intoString:&stringFromArray]) {
            NSLog(@"%@",stringFromArray);
        }
    }

我只是想看看字符串本身是否正确子串 - 但是我在 while 循环开始时得到一个“SIGBART”,错误是这样的:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x8939eb0'

从我在 NSScanner 上看到的所有文档来看,似乎我已经正确设置了它,但是,无论我做什么更改,它似乎都无法开始循环。

我必须做些什么才能正确设置 NSScanner 以避免“SIGABRT”?(为了记录,我假设“SIGABRT”是一个段错误?)。谢谢大家的时间,你们都是最棒的!

(顺便说一句:我知道这对于国家和州城市来说还没有完全实施,我只是想习惯 NSScanner,一旦我控制了 NSScanner,我将实施其余的)

编辑 1:SosBorn!你太不可思议了!非常感谢!所以我已经为我的 viewDidLoad 实现了这个:

- (void)viewDidLoad
{
    self.topPlacesArray = [FlickrFetcher topPlaces];
    NSArray *ArrayOfStrings = [[NSArray alloc] init];
    NSArray  *placeElements = [[NSArray alloc] init];
    NSString *country = [[NSString alloc] init];
    NSString *city = [[NSString alloc] init];
    NSString *state = [[NSString alloc] init];

    ArrayOfStrings = [topPlacesArray valueForKey:@"_content"];

    for (NSString *place in ArrayOfStrings) {

        placeElements = [place componentsSeparatedByString:@", "];
        if ([placeElements count] == 3 && [placeElements objectAtIndex:0] != nil) {
           city = [placeElements objectAtIndex:0];
           [self.cityArray addObject:city];
           state = [placeElements objectAtIndex:1];
           [self.stateArray addObject:state];
           country = [placeElements objectAtIndex:2];
           [self.countryArray addObject:country];
           NSLog(@"%@, %@, %@", city, state, country);    
        }
       else {
           NSLog(@"Did this work?");
       }
    }

    [ArrayOfStrings release];
    [placeElements release];
    [country release];
    [city release];
    [state release];
    [super viewDidLoad];
}

这就像一个完整的魅力但是我在尝试访问时在代表中进行了一些错误的访问self.window.rootViewController = self.viewController - 这没有任何意义(我实际上有一个完全空的桌子,等等......) - 所以我在想我用我的子字符串处理了糟糕的内存管理,现在这个委托调用遇到了麻烦。

Chuck,我对您的评论非常感兴趣,因为我被告知创建变量的正确方法是调用 [myclass alloc] init];然后在你完成后释放——就像我一样。当然,我的 Objective-C 绿度有点……脸红

你们所有人和这个令人难以置信的社区对我们学生来说都是一笔财富——感谢你们所有的时间和奉献精神。进步的唯一途径是合作之路!

编辑2:好的——现在它已经完全修复了,没有可怕的泄漏问题。查克你是对的!我完全混淆了 alloc init 的原理——这是我的最终解决方案:

    NSMutableArray *array1 = [[NSMutableArray alloc] init];
NSMutableArray *array2 = [[NSMutableArray alloc] init];
NSMutableArray *array3 = [[NSMutableArray alloc] init];

self.cityArray = array1;
self.countryArray = array2;
self.stateArray = array3;

[array1 release];
[array2 release];
[array3 release];


NSArray *ArrayOfStrings = [topPlacesArray valueForKey:@"_content"];
NSArray *topPlaces = [NSArray arrayWithArray:ArrayOfStrings];
NSArray *topPlacesSorted = [topPlaces sortedArrayUsingSelector:@selector(compare:)];
ArrayOfStrings = topPlacesSorted;

for (NSString *place in ArrayOfStrings) {
    NSArray *placeElements = [place componentsSeparatedByString:@", "];
    if ([placeElements count] == 3 && [placeElements objectAtIndex:0] != nil) {

        NSString *city = [placeElements objectAtIndex:0];

        [self.cityArray addObject:city];

        NSString *state = [placeElements objectAtIndex:1];

        [self.stateArray addObject:state];

        NSString *country = [placeElements objectAtIndex:2];

        NSString *stateAndCountry = [NSString stringWithFormat:@"%@, %@", state, country];

        [self.countryArray addObject:stateAndCountry];

        NSLog(@"%@, %@, %@", city, state, country);    
    }
   else {
       NSLog(@"Nil Request");
   }

再次感谢 SosBorn,我感觉我忘记了 CS ಠ_ಠ 的基础知识。唯一真正困扰我的是为什么我们必须以这种方式初始化实例 NSMutableArrays ——我发现这是让它们实际工作的唯一方法。

4

1 回答 1

1

不完全确定它为什么会崩溃,但我认为另一种方法会更好地为您服务。你有一个 topPlacesArray,为什么不遍历数组并分别处理每个数组条目呢?我对 topPlacesArray 做了一些假设,但它看起来像这样:

for (NSString *place in topPlacesArray)
{
  //Place is probably in this format: "Country, State, City"
  NSArray *placeElements = [place componentsSeperatedByString:@","];
  //This should give you an array with three elements. Country State and city.
  NSString *country = [placeElements objectAtIndex:0];
  NSString *cityState = [NSString stringWithFormat:@"%@, %@", country, cityState];
  //Now you have your strings that you need. Do whatever you need to do with them.
  //Add them to an array or set the value of a text label, etc.
}

没有花时间处理内存管理,但你明白了。

于 2011-07-08T06:23:04.363 回答