Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS/Objective-C:尝试扫描字符串以查找将分配给多个NSString的子字符串_Objective C_Ios_Nsstring_Nsscanner_Substring - Fatal编程技术网

iOS/Objective-C:尝试扫描字符串以查找将分配给多个NSString的子字符串

iOS/Objective-C:尝试扫描字符串以查找将分配给多个NSString的子字符串,objective-c,ios,nsstring,nsscanner,substring,Objective C,Ios,Nsstring,Nsscanner,Substring,我正试图完成斯坦福iPhone编程(FA10)的任务“Flickr Fetcher”——到目前为止一切进展顺利,但我陷入了僵局: 我已经成功地提取了“前100名”图片的位置,这些图片以字符串形式排列为“国家、州、城市”。我想创建两个NSString——一个是国家,另一个是州和城市。从那里我可以做什么 cell.textLabel.text = countryString; cell.detailTextLabel.text = stateCityString; 在我的表视图数据源方法中 sta

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

我已经成功地提取了“前100名”图片的位置,这些图片以字符串形式排列为“国家、州、城市”。我想创建两个NSString——一个是国家,另一个是州和城市。从那里我可以做什么

cell.textLabel.text = countryString;
cell.detailTextLabel.text = stateCityString;
在我的表视图数据源方法中

stackoverflow和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循环的beggining处得到了一个“SIGBART”,错误如下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x8939eb0'
从我在NSScanner上看到的所有文档来看,似乎我已经正确设置了它,但是,无论我做了什么更改,它似乎都无法开始循环

如何正确设置NSScanner以避免“SIGABRT”?(为了记录在案,我假设“SIGABRT”是一个segfault?)。谢谢大家抽出时间,你们都是最棒的

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

编辑1:索斯伯恩!你真是不可思议!非常感谢你!因此,我为我的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");
   }
再次感谢你,索斯伯恩,我觉得我忘记了CS的基本知识ಠ_ಠ.
唯一让我感到困扰的是,为什么我们必须以这种方式初始化实例NSMutableArray——我发现这是让它们真正工作的唯一方法。

不完全确定它为什么会崩溃,但我认为另一种方法会更好地为您服务。如果您有一个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.
}

没有花时间来处理内存管理,但你知道了。

FYI,代码中的每一个alloc init充其量是毫无意义的,充其量是泄漏。您只需将新对象重新分配给变量,而不使用您创建的对象。