0

これが私の質問です:

viewDidLoadメソッドで変数を作成しますNSUserDefaults(「最初の実行」の場合は、変数を作成して入力しますNSNumber。次に、別のメソッドでそれを使用しようとしますが、...何もありません。空のように見えます。 誰でも助けてくれますか?たくさんの感謝

- (void)viewDidLoad {

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];

if ([defaults objectForKey:@"seriesBool"]!=nil)

{
    seriesBool = [defaults objectForKey:@"seriesBool"];

}

else

{
    int i;

    seriesBool = [NSMutableArray arrayWithCapacity:9];

    for(i=0; i<9; i++)

    {
       [seriesBool addObject:[NSNumber numberWithBool:YES]];

    }

}


-(IBAction)toAction:(id)sender
{
NSLog(@"array: %@", seriesBool);
}

seriesBool が空です...

4

2 に答える 2

3

次のようなプロパティを設定する必要があります

@property(nonatomic,retain) NSMutableArray * seriesBool;

そしてそれを使う

self.seriesBool = [NSMutableArray arrayWithCapacity:9];

また

使用する

 seriesBool = [[NSMutableArray alloc] initWithCapacity:9];

それ以外の

seriesBool = [NSMutableArray arrayWithCapacity:9];

& オブジェクトを割り当てて割り当てる必要があります

seriesBool = [[NSMutableArray alloc] init];
seriesBool = [defaults objectForKey:@"seriesBool"];
于 2012-08-07T12:47:21.883 に答える
1

まず、メモリ管理の基本を学ぶ必要があります。そこで起こっていることは、基本的に、seriesBool iVar を保持していないということです。

こちらをご覧ください: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

iVar を保持するか、より良い方法として、strong/retain プロパティを作成し、アクセサー メソッドを使用してください。

于 2012-08-07T12:18:56.970 に答える