#import <>: global include #import "": local include #import: only include once #include: may recursive include @import: done automatically
Edit Scheme
定义不同的宏
NSAssert
多用断言
打包和解包
打包:头 + 长度 + 内容。
1234567891011
-(NSData*)packageData:(NSData*)data{NSAssert(data&&data.length>0,@"Data cannot be empty");NSMutableData*payload=[NSMutableDatadata];intlength=(int)data.length;[payloadappendBytes:kMagicHeaderlength:4];[payloadappendBytes:&lengthlength:4];[payloadappendData:data];returnpayload;}
-(void)start{NSNetServiceBrowser*browser=[[NSNetServiceBrowseralloc]init];browser.delegate=self;[browsersearchForServicesOfType:@"_http._tcp."inDomain:@""];}-(void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowserdidFindService:(NSNetService*)aNetServicemoreComing:(BOOL)moreComing{if([self.serviceTypescontainsObject:aNetService.name]){NSLog(@"Found a service: %@",aNetService);aNetService.delegate=self;[aNetServiceresolveWithTimeout:5];}}-(void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowserdidRemoveService:(NSNetService*)aNetServicemoreComing:(BOOL)moreComing{}-(void)netServiceDidResolveAddress:(NSNetService*)netService{if([self.serviceTypescontainsObject:netService.name]){[self.delegateserviceAdded:netService];}NSURL*serviceURL=[NSURLURLWithString:[NSStringstringWithFormat:@"http://%@:%li",netService.hostName,(long)netService.port]];NSLog(@"Resolved address for service %@: %@",netService,serviceURL);}-(void)netService:(NSNetService*)senderdidNotResolve:(NSDictionary*)errorDict{NSLog(@"Couldn't resolve address for service %@: %@",sender,errorDict);}