English 中文(简体)
核心数据中的随机字(NSString)
原标题:Random word From Core-data (NSString)

我有一个核心数据模型,它有一个名为Goodie的实体,属性为thingsYouWant,类型为String。

当你按下一个按钮时,我想从“thingsYouWant”中随机选择一个单词,并将其放入一个带格式的字符串中。

但我一直在获取一个NSString,它可能不会响应objectAtIndex错误-(

更新:

这是我的工作代码:

-( void ) viewDidLoad {

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription
    entityForName:@"Goodie" inManagedObjectContext:
             self.managedObjectContext ]];

    NSError *error = nil;
    NSArray *array = [self.managedObjectContext 
                    executeFetchRequest:request error:&error];

    if (array == nil)
    {
        // Deal with error...
    }

    if(array.count > 0){
              int r = random()% [array count];
       goodie = [array objectAtIndex:r];

    } else { // no one to fetch - generate one
        goodie = [NSEntityDescription
            insertNewObjectForEntityForName:@"Goodie"
            inManagedObjectContext:self.managedObjectContext ];
    }



- (void) winText {          

NSArray *components = [[goodie thingsYouWant] 
                          componentsSeparatedByString:@" "];

NSInteger randomIndex = (random() % [components count]);
NSString *newWord = [components objectAtIndex:randomIndex];


winLabel.text =[NSString stringWithFormat: @"Congratulations, 
               the carrot you have earned is -->>> %@", newWord];

}

谢谢:-D

斯科夫

最佳回答

首先,<code>array.count</code>是对点语法的不当使用count不是NSArray的属性,而是一个方法调用。

第二,哪一行出现错误?你是在[array objectAtIndex:0]还是在[goodie.thingsYouWant objectAtIndek:R]thingsYouWant属性的定义。我怀疑它是一个字符串属性。

Update

如果你想从一个字符串中提取一个单词,那么你需要把这个字符串拆分成一个数组。使用方法-componentsSeparatedByString:。然后你可以从那里随机抓一个。

例如:

NSArray *components = [[goodie thingsYouWant] componentsSeparatedByString:@" "];
NSInteger randomIndex = (random() % [components count]);
NSString *newWord = [components objectAtIndex:randomIndex];
问题回答

参考你的这份声明

Goodie with Attribute called thingsYouWant of type String

我和马库斯·S·扎拉在一起

请查看非标准持久属性,用于将数组存储在核心数据中。





相关问题
How do you create UIBarButtonItems with a radio interface?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up ...

iPhone settings bundle

I want to allow the user to enter a valid date using the iPhone’s settings application. I have experimented with many of the PreferenceSpecifiers data node types including date. I have two issues: ...

Circular #import/@class problem in ObjectiveC

I m going to use an example to properly illustrate my confusion. I can t quite wrap my head around this. In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Cocoa-Touch: issue looping MPMoviePlayerController

I have an app which has to load some data at startup, so I want to display a splash-screen animation. I m using the MPMoviePlayerController to play a m4v file. The movie has it s background set to [...

Iphone sequential animation with setAnimationDelay

I m trying to chain animation events. The application I m coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "...

热门标签