I am having this issue in Xcode 6. It is fine if I use this in Xcode 5 or below.
Objects are automatically instantiated even though it's nil and can't check nil.
NSString *userID = [DataManager sharedInstance].currentUser.userId;
if (!userID) {
userID = #"user";
}
[userInfo setObject:userID forKey:LogDataUserIDKey];
That's what I am doing and I am getting EXE_BAD_ACCESS(code=1, address=0x443b800c) at the last line.
userID is instantiated as my attached screenshot. How can I check whether userID is nil. I am lost. Please advise.
OK. I am able to fix the issue. It is because inside this User object which is [DataManager sharedInstance].currentUser, userId is declared as both instance variable and property.
#interface User : NSObject <NSCopying>{
NSString *userId; // instance variable
}
#property (nonatomic, retain) NSString * userId;
So, by removing the instance variable declaration and it fixed the issue.
#interface User : NSObject <NSCopying>
#property (nonatomic, retain) NSString * userId;
But, I am not sure why. You guys have any idea?
Related
This question already has answers here:
ios/objective c/singleton: Storing userid in session variable
(3 answers)
Closed 7 years ago.
This should not be that hard but I cannot get it to work. IOS newb trying to set userid in session instance following the answer by Ismael here. But cannot access value of userid in other class.
Here is the code I am using:
Session.h
#interface IDSession : NSObject
#property (readonly, copy) NSString *userid;
+ (IDSession *)sharedInstance;
#end
Session.m
#import "IDSession.h"
#interface IDSession()
#property (readwrite,copy)NSString * userid;
#end
#implementation IDSession
+ (IDSession *)sharedInstance {
static IDSession *session;
if (!session){
session = [[IDSession alloc] init];
//include this class in other class and reference userid with [IDSession sharedInstance].userid
NSString * userid = #"1";
}
return session;
}
#end
in retrieving class.
#import "session.h"
NSString *userid =[Session sharedInstance].userid;
NSLog(#"userid retrieved from session variable is %#",userid);
The value that appears in log is (null)
Thanks in advance for any suggestions.
You are setting a local userid variable, not the property of the singleton.
Rather than:
NSString *userid = #"...";
You want:
session.userid = #"...";
This is just the Core-Data object for message:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Account;
#interface Message : NSManagedObject
#property (nonatomic, retain) NSNumber * read;
#property (nonatomic, retain) NSDate * sentDate;
#property (nonatomic, retain) NSString * text;
#property (nonatomic, retain) NSString * receiver;
#property (nonatomic, retain) NSString * sender;
#property (nonatomic, retain) Account *account;
#end
Then this is the Messages ViewController, load messages code:
- (void)loadMessages
messages = [[NSMutableArray alloc]initWithArray:_fetchedResultsController.fetchedObjects];
//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.
static NSString *senderId = #"2";
static NSString *senderDisplayName = #"a";
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
senderDisplayName:senderDisplayName
date:[NSDate distantPast]
text:#"helloloo"];
messages = [[NSMutableArray alloc]initWithObjects:message, nil];
//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.
Now I'm new to this framework https://github.com/jessesquires/JSQMessagesViewController , I went all over the framework docs and didn't find answer for my problem which is:
Can I use (NSFetchedResultsController) with this framework?
If so how can I use the (NSFetchedResultsController) in the JSQMessages CollectionView DataSource in the right way and return all my Messages from my core data?. I know how to use (NSFetchedResultsController) but not so sure how to use it with the CollectionView and with this framework.
It seem like the CollectionView data source/delegate methods only work with NSmutableArray and the app crashes when I do staff with (NSFetchedResultsController).
For example:
return [_fetchedResultsController.fetchedObjects objectAtIndex:indexPath.item];
VS:
return messages[indexPath.item];
When I use the _fetchedResultsController.fetchedObjects my app crashes.
So after thinking what to do I said ok what if I will convert my .fetchedObjects (NSarray) to be my (NSmutablearray) of the messages and then I got stack I just don't know how to do this the right way I need help.
All I need is just a way to return all my messages from my SQlite database to my app.
I also have looked at Parse example https://github.com/relatedcode/RealtimeChat , here but I want my own Code without having to pay to Parse.com and to be able to use Core-Data.
When I call this : JSQMessage I need some how to tell it to return all my objects in my fetch result controller *.fetechedobjects*.
Heyo Guys
So I am more or less new to Objective C and got to a problem which I seem unable to solve.
I created a class called "Students" which all have a name surname etc. All those Students are put into a NSMutableArray (before they get created from JSON , that seems to work without a problem though). Then all the names of the students are put into a ListView. Afterwards when a name is clicked (segue passes the object), one should see the details of said student (i.e. his full name, his id, his street).
The problem is that all the string values of the student object seem to get lost.
I checked that all the student object work just fine. I think the problem lies at the #property in my student class.
Any comments and suggestions are appreciated
This is an excerpt of student.h As said only the string values get lost, the int (here the plz value) remains correct
#property (nonatomic, weak)NSString *lastname;
#property (nonatomic, weak)NSString *surname;
#property (nonatomic, weak)NSString *street;
#property (nonatomic, assign)int plz;
EDIT:
Here is where i parse my json.
for (NSDictionary *dic in jsonArray){
NSNumber *identity = [dic valueForKey:#"id"] ;
NSString *firstName = (NSString*) [dic valueForKey:#"first_name"];
NSString *lastName = (NSString*) [dic valueForKey:#"last_name"];
NSString *street = (NSString*) [dic valueForKey:#"street"];
NSNumber *plz = [dic valueForKey:#"plz"] ;
NSString *birth = (NSString*) [dic valueForKey:#"date_of_birth"];
NSArray *bills = dic[#"bills"];
NSArray *hours = dic[#"hours"];
NSLog(#"First %#",firstName);
Student *student = [[Student alloc]initWithLastName:lastName withSurname:firstName withStreet:street withPLZ:plz withOrt:#"Uitikon" withBirthDate:birth withOccupation:#"Schüler"];
[ApprenticeList addObject:student];
}
EDIT 2 :
I found out that the string values get lost even before the segue. All these objects are created in
ViewDidLoad
But in
prepareforsegue
all the values are allready null (except for the int) .So the only place where the student objects work is in
ViewdidLoad
#property (nonatomic, weak)NSString *lastname;
#property (nonatomic, weak)NSString *surname;
#property (nonatomic, weak)NSString *street;
change to
#property (nonatomic, strong)NSString *lastname;
#property (nonatomic, strong)NSString *surname;
#property (nonatomic, strong)NSString *street;
You can also use 'copy'. It will cause the setter for that property to create a copy of the object, otherwise it is identical to strong.
How do I deserialise the object which got modified in new version.
For Example
I have a person class in my project for version 1.0.
Array of Person objects gets serialized when app is terminated
#interface Person : NSObject <NSCoding>
#property NSString *name;
#property NSUInteger age;
#property NSString *location;
#end
This is how I am serialising my data
- (void)serializePersonList {
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:personList];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:kPersonKey];
[defaults synchronize];
}
Now I am planning to release version 2. And Person Class has been evolved to
#interface Person : NSObject <NSCoding>
#property NSString *name;
#property NSUInteger age;
#property NSString *phone;
#property KSAddress *location;
#end
Note
Type of location has beed updated to KSAddress
phone attribute has been added.
How do I make my app deserialize the old Person data ?
Hopefully you're using a keyed archiver. You write the code to connect the archived data for each key to the instance variable it belongs to. As you have changed type your code now needs to read the data into an id local variable, check the data type (isKindOfClass) and then either store the value or convert it to your new format and then store it.
I have a custom object: Vendor that extends NSObject. I am initiating it like so:
NSDictionary *vendorObj = [vendors objectAtIndex:i];
Vendor *vendor = [[Vendor alloc] initWithVendorInfo:vendorObj];
NSLog(#"VendorObj: %#", vendorObj);
NSLog(#"Vendor: %#", vendor);
Here is what the class looks like:
#interface Vendor : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *description;
- (id)initWithVendorInfo:(NSDictionary *)vendorDetails;
#end
#implementation Vendor
- (id)initWithVendorInfo:(NSDictionary *)vendorDetails
{
self = [super init];
if(self)
{
_name = [vendorDetails[#"company_name"] copy];
_description = [vendorDetails[#"description"] copy];
}
return self;
}
If I NSLog vendorObj all the details are there. Once I initiate the Vendor object and NSLog it, the log shows:
2013-11-21 22:22:44.769 [48202:a07] Vendor:
I cannot seem to figure out why my object is nothing, no memory address, not even a null. What am I doing wrong here?
The problem is your description property. The NSObject class defines a description method. This method is called when you use a %# format specifier with an object.
Your description property is overriding that method.
Rename your description property to something else.