我正在关注 Ray Wenderlich 网站上的教程,我试图理解这段代码,它具有 nsarray 只读和 nsmutableble 数组,名称相同但可写。还有什么是使用 _(underscore)property 与设置属性和使用 self.x 的原因 这是代码:这是来自网站
对接会服务器.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
婚介服务器.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;
}
我正在做这样的事情
婚介服务器.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
婚介服务器.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];
}
我在做什么基本上是一样的,否则它不会工作。谢谢!