ios - NSRange exception error to a nsmutablearray -
i new ios
. need create array of specific size , set elements 0. need add array in nsuserdefaults
.
i did following:
//declare , initialize array nsstring *name=@"test"; int y=5; nsmutablearray *testarray = [[nsmutablearray alloc] initwithcapacity:100]; (int i=0; i< [testarray count];i++) [testarray addobject:0]; //initialize elements 0 [[nsuserdefaults standarduserdefaults] setobject:testarray forkey:name]; //read array form nsuserdefaults nsmutablearray *testarray = [[[nsuserdefaults standarduserdefaults] objectforkey:name] mutablecopy]; [testarray replaceobjectatindex:y withobject:[nsnumber numberwithint:1]]; //here nsrangeexception index beyound bounds.
this should work you:
//declare , initialize array nsstring *name=@"test"; int y=5; nsmutablearray *testarray = [[nsmutablearray alloc] init]; (int i=0; i< 100;i++) [testarray addobject:@"0"]; [[nsuserdefaults standarduserdefaults] setobject:testarray forkey:name]; //read array form nsuserdefaults nsmutablearray *testarray1 = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] arrayforkey:name]]; [testarray1 replaceobjectatindex:y withobject:@"1"];
Comments
Post a Comment