Ios NSURL没有可见的@interface声明选择器组件SeparatedByString
NSArray声明带来一个错误,因为“NSURL没有可见的@interface声明选择器组件SeparatedByString”Ios NSURL没有可见的@interface声明选择器组件SeparatedByString,ios,objective-c,nsurl,Ios,Objective C,Nsurl,NSArray声明带来一个错误,因为“NSURL没有可见的@interface声明选择器组件SeparatedByString” NSURL*MyURL=[[NSBundle mainBundle] URLForResource:@“艺术列表”,扩展名:@“txt”]; NSArray*行=[MyURL组件由字符串分隔:@“\n”];//每行,调整行尾字符 对于(int i=0;i
NSURL*MyURL=[[NSBundle mainBundle]
URLForResource:@“艺术列表”,扩展名:@“txt”];
NSArray*行=[MyURL组件由字符串分隔:@“\n”];//每行,调整行尾字符
对于(int i=0;i<10;i++){
NSString*行;
//排队;
NSLog(@“%@,[NSString stringWithFormat:@“行:%@”,行]);
_wordDefBox.text=[NSString stringWithFormat:@“%@%@”,_wordDefBox.text,行];
}
您错过了一步。获得URL后,需要将文件加载到NSString
中。然后调用NSString
上的componentsSeparatedByString
NSURL *myURL = [[NSBundle mainBundle]
URLForResource: @"artList" withExtension:@"txt"];
NSError *error = nil;
// Use the appropriate encoding for your file
NSString *string = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
if (string) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
// and the rest
} else {
NSLog(@"Unable to load string from %@: %@", myURL, error);
}
你漏了一步。获得URL后,需要将文件加载到
NSString
中。然后调用NSString
上的componentsSeparatedByString
NSURL *myURL = [[NSBundle mainBundle]
URLForResource: @"artList" withExtension:@"txt"];
NSError *error = nil;
// Use the appropriate encoding for your file
NSString *string = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
if (string) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
// and the rest
} else {
NSLog(@"Unable to load string from %@: %@", myURL, error);
}
一般来说,当您看到这样一个错误时,它意味着类X(此处
NSURL
)没有任何名为Y的方法(例如组件由字符串
分离),或者至少它的接口中没有它,即它不是公共方法,它可能是私有方法,可用于其实现。始终尝试理解编译器告诉您的内容。要了解更多信息,您可以“Cmmd+单击”任何类,它将带您到它的接口,您可以看到它有哪些公共方法。在NSString
和NSURL
具体来说:
NSURL
没有这种方法。它不属于NSURL
,它属于NSString
一般来说,当您看到这样一个错误时,它意味着类X(这里NSURL
)没有任何名为Y的方法(例如组件由string
分离)或者至少它的接口中没有它,也就是说,它不是它的公共方法,它可能是它的私有方法,并且可用于它的实现。始终尝试理解编译器告诉您的内容。要了解更多信息,您可以“Cmmd+单击”任何类,它将带您到它的接口,您可以看到它有哪些公共方法。在NSString
和NSURL
NSURL *myURL = [[NSBundle mainBundle]
URLForResource: @"artList" withExtension:@"txt"];
NSError *error = nil;
// Use the appropriate encoding for your file
NSString *string = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
if (string) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
// and the rest
} else {
NSLog(@"Unable to load string from %@: %@", myURL, error);
}
具体来说:
NSURL
没有这种方法。它不属于NSURL
,它属于NSString
组件,由字符串分隔:
是NSString
方法,而不是NSURL
方法。我猜您想要的是:+stringWithContentsOfURL:encoding:error:
,然后在其上使用components-paratedbystring:
。components-paratedbystring:
是一个NSString
方法,而不是NSURL
方法。我想您需要的是:+stringWithContentsOfURL:encoding:error:
,然后在其上使用组件,这些组件由string:
分隔。
NSURL *myURL = [[NSBundle mainBundle]
URLForResource: @"artList" withExtension:@"txt"];
NSError *error = nil;
// Use the appropriate encoding for your file
NSString *string = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
if (string) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
// and the rest
} else {
NSLog(@"Unable to load string from %@: %@", myURL, error);
}