last day (14 days later) » 

9:44 AM
0
A: NSUserDefaults setObject method causes the app to crash in ios8

eridbYou should check the UserID type because crash is not related with changing NSArray to NSMutableArray. Hint: Put a breakpoint and inspect what kind of values you store. In Xcode 6.1 GM I wrote this code to store the value: NSMutableArray *yourArray = [[NSMutableArray alloc] initWithObjects:@"...

 
I have edited my question with NSLogs.
 
@Manthan you can't store custom objects's NSMutableArray to userDefaults. You need to represent it as NSData. Ill edit my answer.
 
Ok thanks... but it works for ios6 and ios7 then why this problem comes only for ios8?
 
I don't know how you really passed these custom objects to NSUserDefaults, but this is a well-known issue that you can't pass the objects to NSUserDefaults. If you have NSDictionaries on you array than the logic changes.
 
Actually i got an existing project and i am making some modifications so i am not aware some changes made by other developer. So sorry for that.
When i write this [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:selectedLists] forKey:UserID]; the app still crashes.
 
9:44 AM
You need to add the <NSCoding> to your custom class. For example @interface CustomObject : NSObject <NSCoding>
Hey Manthan
 
Hey Eric...
how can i add the above code in my project?
 
Its Erid but its OK
 
ohh sorry!!! Hi Erid...
 
Look your problem I think is related to your NSNumber value
 
Yeah i think userid in nsnumber
 
9:46 AM
Where do you get that UserID
 
It is coming from an array
UserID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
userID is NSString an users in array
 
I understand
 
then i set this value of userID in defaults
[[NSUserDefaults standardUserDefaults]setValue:UserID forKey:@"UID"];
 
but thats an object
 
UserID = [[NSUserDefaults standardUserDefaults]valueForKey:@"UID"];
 
9:50 AM
how do you know that has NSString value
I mean the first one
UserID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
 
wait... I'll show u the value of users array
 
Ok
 
{
FirstName = m;
ImagePath = "/Documents/User_1.jpg";
LastName = P;
UserID = 1;
}
 
NSString *userID;
if ([[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] isKindOfClass:[NSNumber class]])
{
userID = [[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] stringValue];
}
//If is string
else
userID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
try this
compare the class of your object with key UserID
 
k wait...
 
9:54 AM
if thats a NSNumber you need to convert it to NSString
if thats a NSString thats all right
because you may be using NSString encoded to NSNumber on NSUserDefaults key which needs to be NSString
 
yes that is the problem i guess
u r right.
 
Does it work now?
 
wait m checking
running the project
 
Is the app running well
 
ya bt it crashes in the below code
in my app
 
10:06 AM
what code
?
 
the code below the values that u shown
listsArray = [[database executeQuery:[NSString stringWithFormat:@"select * from tbl_List where UserID = %@ order by ListName ASC",UserID]] mutableCopy];
//NSLog(@"listarray %@",listsArray);
[database close];
NSArray *previousSelected = [[NSUserDefaults standardUserDefaults] objectForKey:UserID];
// NSLog(@"PreviousSelected : %@",previousSelected);
checkedLists = [[NSMutableArray alloc] initWithCapacity:[listsArray count]];

for (int i=0; i<[listsArray count]; i++)
{
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
the app crashes on for loop
line
 
put a breakpoint after listarray
inspect it step by step
its easy
we need to know what value has UserID
 
yup that was what i was looking for
 
are debugging it?
 
If i remove your code then the above works well
yes yes
it solves the crash bt the values are not passing in array
 
10:14 AM
Yes i know that because your userID is not being queried on the database
NSString *userID;
if ([[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] isKindOfClass:[NSNumber class]])
{
userID = [[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] stringValue];
}
//If is string
else
userID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
sorry for the above code
NSString *userID;
if ([[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] isKindOfClass:[NSNumber class]])
{
userID = [[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] stringValue];
NSLog(@"It of type NSNumber");
}
//If is string
else
{
userID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
NSLog(@"It of type %@",[userID class]);
}
add this and tell me your log
what does the log say before the crash
 
k
wait...
NSLog(@"It of type NSNumber");
 
Okay now add a NSLog(@"%@",userID); just before NSLog(@"It of type NSNumber");
and execute it again
tell me the log value
 
k
1
 
the number is passed correctly
now you use that userID string to the query
because, if there's an error with your query
than its an error on your database
 
ok man!!! thanks..
I'll try that....
 
10:26 AM
above listarray add this NSLog("Query: select * from tbl_List where UserID = %@ order by ListName ASC",UserID);
to test if the query is right
if the query is right then your answer was related to NSNumber crash instead of NSString when searching for NSUserDefaults with key of type NSNumber
 
query in ri8
I have checked that.
 
then, you need to ask another question related to your problems with database
 
Problem is with NSNumber and NSString
one thing i want to ask is that then why this code works for ios6 and ios7
 
okay show me the loop code
when gives crash
 
I'll tell u what is actually heppening
On my view from one side, i select an exam paper and user name which has user id.
So on that
view exam paper comes for that user
 
10:32 AM
I understand but I am not trying to learn the idea, your idea that you are dealing with NSNumber and NSString in another place
so everytime check when you are passing NSString or NSNumber by the conditions I gave you
 
Ohhk buddy... Thanks for your help....
Really appreciate this... +1 for this...
 
Its okay man
Ill edit the question to the one which deals with your questions
Have a nice day :)
 
Thanks... Same to u...
Will ping u again if i need help for any other questions...
Hope u don't mins
mind*
IF i write UserID = [[NSString alloc]init]; before [[NSUserDefaults standardUserDefaults] setObject:selectedLists forKey:UserID]; na then also the app doesn't crash but values are not passed
@eridb
 
11:05 AM
Its ok to make questions to me, Ill help you from what I know :)
look that happens because you are initializing again the userID string. Initializing means you have allocated memory for it (It's not null) but it is empty
you can check it by doing this
if(userID.length>0)
then the string is not empty
else
the string is empty
is your occasion is empty
the crash I mention again is related to your database
check the connections
 
ohhkk
 
11:19 AM
Are u aware of how to upload image to webserver?
@eridb
 
11:51 AM
Yes
you can use multiparting
or
you can convert your image to base64string
 

last day (14 days later) »