0

I copied the following from a tutorial where it did not cause an error into my project where it causes three errors on the line indicated. (The errors don't seem correct as when I try to fix them, other errors occur.) Can anyone suggest what might be problem:

@interface VC ()
   NSDictionary(JSONCategories) //MULTIPLE ERRORS THIS LINE including cannot declare variables inside @interface
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

2 Answers 2

3

This is an NSDictionary category; you want:

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

Apple Reference.

Sign up to request clarification or add additional context in comments.

6 Comments

This is in a large class file. Can you have more than one interface or is it okay to use the regular interface and just add the category even though you have other things like properties in the interface?
@user1904273 I'm not sure I follow your question. Normally you would have the above code in a file called NSDictionary+JSONCategories.h and the implementation in NSDictionary+JSONCategories.m and just implement what's necessary. It's not normal to lump many such categories into a single header/implementation file.
I just created second interface ...end and implementation ..end in the view controller and that fixed errors. This is what was done in tutorial. Is this unusual? Actually, I'm much more familiar with +category approach. Should I create separate + category and then import it into this VC?
Yeah I'd say it's much more useful to define the categories in their own header/implementation file as that means it can be easily copied between projects.
When you make the category do you subclass the VC or what do you subclass? I tried subclassing NSObject and it gave it the name NSOBject.... so that does not seem like way to go.
|
3

I assume you are trying to create a category:

a category interface looks exactly like a normal interface, except the class name is followed by the category name in parentheses.

For example Categories should be declared as:

#import "Car.h"

@interface Car (Maintenance)   //Maintainence is a category

- (BOOL)needsOilChange;
- (void)changeOil;
- (void)rotateTires;
- (void)jumpBatteryUsingCar:(Car *)anotherCar;

@end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.