I am following a tutorial from Ray Wenderlich site and i am trying to understand this code where it has nsarray readonly and nsmutableble array with the same name but writable. Also what is the reason to use _(underscore)property vs setting a property and using self.x Here is the code: this is from the site
MatchmakingServer.h
@interface MatchmakingServer : NSObject <GKSessionDelegate>
@property (nonatomic, assign) int maxClients;
@property (nonatomic, strong, readonly) NSArray *connectedClients;
@property (nonatomic, strong, readonly) GKSession *session;
- (void)startAcceptingConnectionsForSessionID:(NSString *)sessionID;
@end
MatchmakingServer.m
#import "MatchmakingServer.h"
@implementation MatchmakingServer
{
NSMutableArray *_connectedClients;
}
@synthesize maxClients = _maxClients;
@synthesize session = _session;
- (void)startAcceptingConnectionsForSessionID:(NSString *)sessionID
{
_connectedClients = [NSMutableArray arrayWithCapacity:self.maxClients];
_session = [[GKSession alloc] initWithSessionID:sessionID displayName:nil sessionMode:GKSessionModeServer];
_session.delegate = self;
_session.available = YES;
}
I am doing something like this instead
matchmakingserver.h
@interface zvMatchMakingServer : NSObject <GKSessionDelegate>
@property (nonatomic, assign) int maxClients;
@property (nonatomic, strong, readonly) NSArray *connectedClients;
@property (nonatomic, strong, readonly) GKSession *session;
- (void)startAcceptingConnectionsForSessionID:(NSString *)sessionID;
@end
matchmakingserver.m
@interface zvMatchMakingServer()
@property (nonatomic,strong) NSMutableArray *connectedClients;
@property (nonatomic, strong) GKSession *session;
@end
@implementation zvMatchMakingServer
-(NSArray *)connectedClients
{
return self.connectedClients;
}
-(void)startAcceptingConnectionsForSessionID:(NSString *)sessionID
{
self.connectedClients = [[NSMutableArray alloc]initWithCapacity:self.maxClients];
self.session = [[GKSession alloc]initWithSessionID:sessionID displayName:nil sessionMode:GKSessionModeServer];
}
Is what i am doing basically the same thing or it won't work. Thank you!
@synthesize
directive. The functionality has not changed.