7

I have an Xcode 3.2 targeted project . I want to convert this project to ARC to improve the

performance.I am following these steps.

Edit->Refactor->Convert to Objective C ARC

I have got some .mm files to which I am disabling the ARC using -fno-objc-arc. But even after

this I am getting lot of errors (ARC issues). For eg mainly in self=[super init]call,the

error is cannot assign to self outside of a method in the init family. Can

anyone tell me am I following the correct steps ?

Jon Hess
  • 14,237
  • 1
  • 47
  • 51
Raj
  • 1,119
  • 1
  • 15
  • 32

2 Answers2

15

I was trying to convert a projet to ARC and after creating a new one and including the files from the old one - one of the issues i got was

Cannot assign to 'self' outside of a method in the init family

The selector name MUST begin with init - not only that - in my case the init selector was:

-(id)initwithPage:(unsigned)pageNum {...}

Notice the small 'w'.

I have changed it to:

-(id)initWithPage:(unsigned)pageNum {...}

Notice the capital 'W'!

My problem was solved.

I hope this helps someone.

Sasho
  • 3,532
  • 1
  • 31
  • 30
  • 1
    Even I have observed it in my XCode as well. – Raj Dec 04 '12 at 14:26
  • 1
    An additional note: this is mentioned (albeit very subtly) in LLVM's documentation: "A selector is in a certain selector family if, ignoring any leading underscores, the first component of the selector either consists entirely of the name of the method family or it begins with that name followed by a character **other than a lowercase letter.**" – Xono Dec 07 '12 at 02:29
  • and do not forget to -(id) the return value! – JeanNicolas Mar 05 '13 at 12:48
1

Ray Wenderlich's tutorials have a pretty good ARC-conversion two-part tutorial which should detail the right steps to take: Part 1 Part 2

In terms of the error you're having, the description pretty much sums it up: you're assigning to self in a method which doesn't meet the standard for init methods. I can't speak for whether that's legal pre-ARC but it certainly isn't anymore. To resolve that one, either make it an init method (follows the init practise of calling a super init method into self, initialising any values, and returning self), or take out the assignment to self inside it. For other errors, post any you're having an issue with (many of them are covered in Ray's tutorials), and we'll do what we can.

Xono
  • 1,900
  • 17
  • 20
  • 1
    It's totally legal, but the question is: in what scenario would you want to assign `self` outside of an `init` method? – nielsbot Sep 17 '12 at 06:49
  • 1
    It's entirely possible his methods have init-style behaviour, but simply aren't returning the init-ed object (i.e calling `obj = [Obj alloc];` `[obj initWithData];` with initWithData returning void). Because init methods must return a pointer, it would function fine pre-ARC, but cause mass problems when converted. Luckily, if that's all it is, it should just be a matter of updating the declarations. – Xono Sep 17 '12 at 07:13