Objective-C. Multiple anonymous categories?
Asked Answered
B

1

6

About anonymous categories:

But can't I declare an anonymous category for the second time to split my variables' and methods' definitions?

I know that Xcode allows to do so, but will it work without problems?

UPDATED

I explain againg. The main question is can I use 2 categories without name (anonymous) in the same file or will they override/overlap each other? Is it clear now?

Budworth answered 17/1, 2014 at 15:6 Comment(3)
You're talking about extension categories? And you want 2 of them? You can't.Kosygin
Why would you even declare methods in the method extension? Note that you are not required to declare method (note it's "declaration", not "definition". "definition" is where you put your code).Headlong
@Headlong See “Option 3” of this answer for an example.Thoughtful
T
19

You can declare as many class extensions as you want. Each can contain instance variables and methods. As long as the compiler has seen all of the class extensions before it sees the @implementation of the class, it will work.

You should have one source file (with extension .m or .mm) that contains the @implementation of your class. That source file needs to contain all of the class extensions before the @implementation, so if you put your extensions in .h files, you need to include those .h files in that .m file.

UPDATE

To explicitly answer the question in your update: Yes. You can use two class extensions (anonymous categories) in the same file. Your class will contain the instance variables declared in both extensions, as long as the compiler sees both extensions in the same translation unit as the @implementation of the class.

Thoughtful answered 17/1, 2014 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.