Objective C 2.0 properties and storage keywords

11.12.2009 21:11 in ObjC

@interface Foo : NSObject
{
  NSString *_foo;
}
@property NSString *foo;
@end

@implementation Foo : NSObject
@dynamic foo;
- (void) setFoo:(NSString*)value
{
  [_foo release];
  _foo = value;
  [_foo retain];
  [self doSomethingFancyWithNewFoo];
}
- (NSString*) foo
{
       return _foo;
}
@end

This code is quite cool. However, a problem arises. Here we go.

warning: 'assign' attribute (default) not appropriate for non-gc object property 'foo'
warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed

OK. We have 3 options:

  • @property (assign) NSString *foo
  • @property (retain) NSString *foo
  • @property (copy) NSString *foo

I assume you know what those values would mean in @synthesize. But do they have any meaning with @dynamic properties?

Answer is: no. They have no meaning at all. I changed NSString to my own class doing a lot of NSLogging and usage of assign/retain/copy is completely meaningless.

I suppose the same goes for (non)atomic keywords. So why do we have to write down those keywords anyway? I have no idea. Should it be a hint for class' user? But dynamic property doesn't need to store its data in variable. I'm aware that class' clients know nothing about implementation (is is synthesized or dynamic). But I think that there should be dynamic storage keyword. So that we could write:

@property (dynamic[, readwrite]) NSString *foo;

If you are Obj-C expert and know better explanation, let me know. :)

Going more Web 2.0

08.12.2009 01:19 in blog

As you see, I've integrated my blog with Twitter. It has a rather nice API (but SOAP is better anyway). As the current layout has no space for fancy stuff like twitter statuses, I've added a Dabroz Labs (rings a bell? *really*?) -- more extras on the way (one of those extras would be an option to disable them).

And if we are going to be more Web 2.0, what should be next? Profiles and photos? Direct messaging and a lot of colorful flash applications? Well, I'd rather steal everything from Facebook and call it... maybe Grono.net? Oh s**t, the name's already occupied. :(

C++ pack 2: Inheritance + initialization lists

02.12.2009 23:21 in c++

I've got quite confused today.

class Foo
{
public:
  int *bar;
  int index;
};

class FooEx : protected Foo
{
public:
  FooEx(int *bar, int index) :
    bar(bar), index(index) {};
};

What's wrong with this code? (of course real example was more complicated with templates & other fun stuff involved) I was convinced that everything is OK, and error message broke my heart:

foo.cpp: In constructor 'FooEx::FooEx(int*, int)':
foo.cpp:13: error: class 'FooEx' does not have any field named 'bar'
foo.cpp:13: error: class 'FooEx' does not have any field named 'index'

And answer is: you can't have superclass' members in initialization list. Instead you must call superclass' constructor with appropriate parameters. Considering that such code as above is not often seen it's not a shame to be defeated by another C++ fancy feature.