Reputation: 275
I came from front-end development, so MVC and OOP still give me some head shakes. Just to explain to you I send like 500 dicionaries (with 100 parameters) to a nodejs server. The problem is that I has creating ivars for each parameter and each dicionary. Now I want to create some classes like a person class, in the same header file that I have my syncronization class for example. I can make something like this on the header:
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"
@class GCDAsyncSocket;
@interface socketDelegate : NSObject<NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSMutableArray *messages;
GCDAsyncSocket *socket;
dispatch_queue_t connectionQueue_;
}
- (void) initNetworkCommunication;
- (void) sendMessage:(NSArray *)message:(int)numberOfContactsToSend;
@end
@interface personInfo: NSObject
@property (nonatomic,weak)NSString*firstName;
@property (nonatomic,weak)NSString*lastName;
@property (nonatomic,weak)NSDictionary*dicToSendWithConctactInfo;
@end
But in the implementation I don't know how to handle the multiple classes. Like I've a method inside the "socketDelegate" class that needs to use the person class, but it's not available inside it.
What's the best way to implement this?
Upvotes: 3
Views: 2124
Reputation: 122538
To answer your immediate question, you can just forward-declare personInfo
at the top of the file before socketDelegate
:
@class personInfo;
Usually you just put each public class in its own implementation and header files, and each implementation file includes the header files of all the classes it uses. The header files usually just need to forward declare the classes they refer to (as you are doing with @class GCDAsyncSocket;
. However, it doesn't make sense that you are both importing #import "GCDAsyncSocket.h"
and forward-declaring. From what you are using it for here, you don't need the import. However, to properly use GCDAsyncSocket
, you will need to implement GCDAsyncSocketDelegate
protocol, which will require you to import the header; however, you should probably implement that protocol as part of a "class extension" inside the implementation file).
You only need to import the header of something in your header if you are subclassing a class, or implementing a protocol that is declared in the header. For all other uses (i.e. using the class or protocol as part of a pointer type), you can simply forward-declare it. (And for implementing a protocol, you can do that in the implementation file with a "class extension" if you don't need people to know you're implementing the protocol.
Upvotes: 3
Reputation: 187272
Different classes should, typically, be in different files. Once PersonInfo
(please capitalize class names) has it's own PersonInfo.h
and PersonInfo.m
, then you simply add
#import "PersonInfo.h"
to the header file above to be able to reference PersonInfo
from your SocketDelegate
class (again, please capitalize class names).
Upvotes: 3