Babiker
Babiker

Reputation: 18808

How to assign Objective-C properties using dot notation?

I'm new to Objective-C. I'm currently testing properties with the following code. Note this is on windows using GNUstep:

#import <Foundation/Foundation.h>

@interface Car : NSObject
    @property NSString *color;
@end

@implementation Car
@end

int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Car *honda = [[Car alloc] init];
    honda.color = @"Red";
    NSLog(@"%s", honda.color);   

    [pool drain]; 
    return 0;
}

But getting the following:

C:\Users\Bab\Desktop\main.m:5:2: warning: object property 'color' has no 'assign', 'retain' or 'copy' attribute; assuming 'assign' [enabled by default]
C:\Users\Bab\Desktop\main.m:5:2: note: 'assign' can be unsafe for Objective-C objects; please state explicitly if you need it
C:\Users\Bab\Desktop\main.m:9:1: warning: incomplete implementation of class 'Car' [enabled by default]
C:\Users\Bab\Desktop\main.m:9:1: warning: method definition for '-setColor:' not found [enabled by default]
C:\Users\Bab\Desktop\main.m:9:1: warning: method definition for '-color' not found [enabled by default]
: Uncaught exception NSInvalidArgumentException, reason: Car(instance) does not recognize setColor:
[Finished in 0.4s with exit code 1]

Upvotes: 0

Views: 364

Answers (3)

user4006718
user4006718

Reputation: 16

The problem is not the notation. The problem is here:

: Uncaught exception NSInvalidArgumentException, reason: Car(instance) does not recognize setColor:

The runtime is looking for the expected "setColor" accessor for the property "color". To generate accessors automatically, use @synthesize as matt said, or create them explicitly in the implementation.

Explanation of synthesize: What exactly does @synthesize do?

However, @synthesize is an objective-c 2.0 keyword which is not supported by the gcc in GNUStep. You need clang to enable this: How can i use Objective-C's Property feature in GNUstep?

I installed clang with this guide: https://solarianprogrammer.com/2012/03/21/clang-objective-c-windows/

FYI this is not an ARC issue - ARC is part of objective-c, not GNUStep as Fred Frith-MacDonald said. ARC can also be used under GNUStep via clang/llvm.

Upvotes: 0

matt
matt

Reputation: 535964

I don't know or care a hoot about GNUStep on Windows. But my guess is that if GNUStep on Windows lacks ARC it lacks autosynthesis of properties. So you would have to say @synthesize explicitly or maybe even write your own accessor methods.

Some further discussion here: http://wiki.gnustep.org/index.php/ObjC2_FAQ#Which_Bits_of_Objective-C_2_Work.3F

Basically it appears that large sections of the "modern runtime" may not be there.

Upvotes: 2

Sinri Edogawa
Sinri Edogawa

Reputation: 331

If you use gcc to compile those codes under mac terminal, you might got these:

SinriMac:overstack Sinri$ gcc 03.m -framework Foundation
03.m:4:5: warning: no 'assign', 'retain', or 'copy' attribute is specified -
  'assign' is assumed [-Wobjc-property-no-attribute]
@property NSString *color;
^
03.m:4:5: warning: default property attribute 'assign' not appropriate for
  non-GC object [-Wobjc-property-no-attribute]
03.m:16:18: warning: format specifies type 'char *' but the argument has type
  'NSString *' [-Wformat]
NSLog(@"%s", honda.color);   
        ~~   ^~~~~~~~~~~
        %@
3 warnings generated.

SinriMac:overstack Sinri$ ./a.out
2014-12-09 00:16:55.804 a.out[86907:5686808] `踺u…
SinriMac:overstack Sinri$ 

So it should be the issue of GNUStep. Use @synthesize is the method to solve this.

PS Anyway, the %s is strange here, why not use %@?

Upvotes: 0

Related Questions