0

After updating to Xcode 6 many incompatible conversion assignment warnings / errors started to appeared

In the .h file:

@property (nonatomic) BOOL *done;

In the .m file:

@synthesize done;
- (id)init
{
if (self = [super init])
{
    self.done = FALSE;
}
return self;
}

- (void) crashed {
    self.done = TRUE;  #this line gives an incompatible type conversion warning
}

Lots of these warnings appeared after the upgrade. Does anyone share similar problem? This is not a localized problem, the issue spread across the entire project. I thought some of my foundation was wrong, or is it ?

Bill Lau
  • 15
  • 5
  • 5
    This is GOOD because you can learn when to _not_ using pointer – Bryan Chen Oct 10 '14 at 01:43
  • 2
    Also `YES` and `NO`, not `TRUE` and `FALSE`. If you are writing Objective-C use the Objective-C conventions. Also @synthesize is no longer necessary. – zaph Oct 10 '14 at 01:46
  • 3
    It appears you need a modern tutorial. The one you are following is out of date. – rmaddy Oct 10 '14 at 03:22

2 Answers2

0

I'm pretty sure you want to do it this way:

Header file

@property (nonatomic) BOOL done;  // Note: NOT a pointer

Implementation file

- (instancetype)init {
    if (self = [super init]) {
        self.done = NO;
    }
    return self;
}

- (void)crashed {
    self.done = YES;
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    Also, Apple recommend `instancetype` instead of `id` as the return type of methods that return an instance of the class they are called on. Doc link: https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html – Robotic Cat Oct 10 '14 at 02:18
0

Not all variables in Objective C have to be declared with the * character as some newcomers to the language think. It is C legacy to show that the variable is a pointer to an object. Some basic types like int and BOOL are too simple to require the overhead of storing them in an object, so C-style primitive types are used. From the docs:

Scalar types are used in situations where you just don’t need the benefits (or associated overheads) of using an object to represent a value. While strings of characters are usually represented as instances of the NSString class, numeric values are often stored in scalar local variables or properties.

BOOL is a primitive data type in Objective C and is not supposed to be a pointer. The warnings are correct. Declare the variable like this:

@property (nonatomic) BOOL done;

Note the lack of the * character next to the variable name. Other primitive types like int or float also should be declared in a similar fashion.

Some other things about your code. The correct Objective C convention for BOOL values is YES instead of TRUE and NO instead of FALSE, so you should stick to it. Also, since Xcode 4.4, you don't need @synthesize outside of a few special cases described here. As pointed out in the comments, it's also better to use instancetype instead of id in your case as described in the docs.

Community
  • 1
  • 1
p4sh4
  • 3,292
  • 1
  • 20
  • 33