0

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. enter image description here

iHulk
  • 4,869
  • 2
  • 30
  • 39
Min Soe
  • 1,214
  • 1
  • 14
  • 25
  • 1
    You do realize that Xcode is just an IDE? This question has nothing whatsoever to do with Xcode. – dandan78 Nov 26 '14 at 07:49
  • have tried to log the userID before setting it in dictionary and after the if statement? – iHulk Nov 26 '14 at 07:54
  • If I put `NSLog(@"userID : %@", userID);` right after `userID` assignment, I also got `EXC_BAD_ACCESS` – Min Soe Nov 26 '14 at 08:03
  • @dandan78 i am having that issue only in Xcode 6. Not in Xcode 5. That's why I put Xcode 6. Xcode is just an IDE i know. but different version of xcode is different version of compiler, right? – Min Soe Nov 26 '14 at 08:04
  • @MinSoe Ok, then that's important info that should've been included in your question. Always include all relevant information. – dandan78 Nov 26 '14 at 08:08
  • @dandan78 ya? you removed it because you think it's irrelevant. – Min Soe Nov 26 '14 at 08:18
  • @MinSoe You weren't clear. You never said it worked fine in the previous version of Xcode. – dandan78 Nov 26 '14 at 08:23
  • @dandan78 my bad for posting unclear question. – Min Soe Nov 26 '14 at 09:10
  • can you tell me what object is your [DataManager sharedInstance] class method returning as your log says that self is representing the DataManager class. But you say, it is giving you the compile time error when replacing [DataManager sharedInstance] with self? – iHulk Nov 26 '14 at 09:13
  • @iHulk this is what inside DataManager's sharedInstance `+ (DataManager*)sharedInstance{ @synchronized(self){ if(!_sharedInstance){ _sharedInstance = [[self alloc]init]; } return _sharedInstance; } return nil; }` – Min Soe Nov 26 '14 at 09:16
  • Have you tried debugging with NSZombies? http://stackoverflow.com/a/4917557/909655 – Mats Nov 26 '14 at 10:18

1 Answers1

0

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?

Min Soe
  • 1,214
  • 1
  • 14
  • 25
  • why are you declaring both the ivar and property? – Nick Nov 27 '14 at 02:56
  • @Nick umm, it was already like that. I inherited the code from someone. – Min Soe Nov 27 '14 at 04:16
  • I'd delete the ivar declaration, and allow xcode to automatically synthesize the property to `_userId`. It avoids the name conflict and you can still get to the ivar if you need to. There is a good chance one of those declarations was in error. – Nick Nov 27 '14 at 04:25
  • @Nick yes. that's what i did. I removed the ivar and it's solved the problem. – Min Soe Nov 27 '14 at 06:02