【发布时间】:2014-08-25 11:56:32
【问题描述】:
我目前正在学习 Objective-C。我正在学习的这本书给了我一个挑战,我得到了两个数组,我应该创建一个程序来显示两个数组中列出的对象。
我已经设置了所有内容,唯一让我非常困扰的部分是比较两个数组并返回它们中出现的内容。它们都是很长的列表,我所有的尝试最终都没有出现,或者崩溃。
任何关于如何做到这一点的帮助都会很棒。
编辑:这是我迄今为止所做的......我不能完全给出我之前尝试过的例子,因为我会使用诸如 isEqualToString:、predicateWithFormat: 之类的东西,并且在它不起作用之后我会删除代码。
如果我的代码包含明显的错误,我深表歉意,让我再次提及我是新人。
{ @autoreleasepool {
// Read in a file as a huge string (ignoring the possibility of an error)
NSString *nameString =
[NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
// Read in a file for words
NSString *wordString =
[NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
// Break it into an array of strings
NSArray *namesArray = [nameString componentsSeparatedByString:@"\n"];
// Break words into an array of strings
NSArray *wordArray = [wordString componentsSeparatedByString:@"\n"];
// Go through the array one string at a time
for (NSString *n in namesArray) {
// Here is where I'm at
}
}
}
【问题讨论】:
-
this的可能重复
-
请提供您尝试过的示例代码。
标签: objective-c arrays nsarray matching