此代码来自一个示例 在.h部分:
@interface ViewController : UITableViewController < NSNetServiceBrowserDelegate > {
NSMutableArray * tableData;
NSNetServiceBrowser * _browser;
NSMutableArray * _foundServices;
NSURLConnection * _connection;
NSInputStream * _consumerStream;
NSString * controllerHostName;
}
@property (nonatomic, retain) NSMutableArray * tableData;
@property (nonatomic, retain) NSNetServiceBrowser * _browser;
@property (nonatomic, retain) NSMutableArray * _foundServices;
@property (nonatomic, retain) NSURLConnection * connection;
@property (nonatomic, retain) NSInputStream * consumerStream;
@property (nonatomic, retain) NSString * controllerHostName;
<。>部分中的
#import "ViewController.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <CFNetwork/CFNetwork.h>
@synthesize tableData;
@synthesize _browser;
@synthesize _foundServices;
@synthesize consumerStream = _consumerStream;
@synthesize connection = _connection;
@synthesize controllerHostName;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_browser = [[NSNetServiceBrowser alloc] init];
[_browser setDelegate:self];
[_browser searchForServicesOfType:@"_service._tcp" inDomain:@""];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_browser stop];
_browser.delegate = nil;
[_browser release];
_browser = nil;
[_foundServices removeAllObjects];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing
{
self.controllerHostName = [NSString stringWithFormat:@"%@.%@", netService.name, netService.domain];
NSLog(@"ControllerHost String is: %@", self.controllerHostName);
NSLog(@"URL to use is === %@.%@", netService.name, netService.domain);
if (!_foundServices) {
_foundServices = [[NSMutableArray alloc] init];
}
[_foundServices addObject:netService];
[self.tableView reloadData];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing
{
[_foundServices removeObject:netService];
}
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser
{
[_foundServices removeAllObjects];
}
我声明了样本的所有标题 但我收到错误消息“无法找到”NSNetServiceDelegate“的协议声明 在.h
interface ViewController : UITableViewController < NSNetServiceBrowserDelegate >
所以我遗漏了什么声明? 该示例没有任何警告或错误
答案 0 :(得分:1)
我认为它是在“NSNetServices.h”中定义的吗?
答案 1 :(得分:1)
答案 2 :(得分:1)
问题是未使用的功能。只需删除:
[self.tableView reloadData];
没关系!
答案 3 :(得分:0)
我找到了类似答案,JUST REMOVE
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_browser stop];
_browser.delegate = nil;
[_browser release];
_browser = nil;
[_foundServices removeAllObjects];
}
该程序将获得我想要的Netservice ... 为什么?