Tom*_*lly 15 objective-c nsmutablearray nsarray ios
我有以下代码,我想使用NSMutable数组而不是NSArray你能告诉我如何加载NSMutable数组,因为当前的方法不起作用.
-(void) whatever{
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *titles = [xpathParser search:@"//h3"]; // get the page title - this is xpath notation
TFHppleElement *title = [titles objectAtIndex:0];
NSString *myTitles = [title content];
NSArray *articles = [xpathParser search:@"//h4"]; // get the page article - this is xpath notation
TFHppleElement *article = [articles objectAtIndex:0];
NSString *myArtical = [article content];
Run Code Online (Sandbox Code Playgroud)
我试过了 :
NSMutableArray *titles = [xpathParser search:@"//h3"];
Run Code Online (Sandbox Code Playgroud)
但它确实加载了这些值?
Jac*_*kin 33
您可以调用mutableCopy一个NSArray对象返回给您NSMutableArray.
请注意,被调用者将获得此对象的所有权,因为方法名称包含"copy".
(Apple的内存管理指南指出,包含单词"alloc","new"或"copy"的方法名称应该按照惯例返回您拥有的对象,因此必须放弃在某些时候的所有权.)
jer*_*jer 31
就像这样:
NSArray* someArray = [xpathParser search:@"//h3"];
NSMutableArray* mutableArray = [someArray mutableCopy];
Run Code Online (Sandbox Code Playgroud)
这完全是字面意思.
假设[xpathparser ...]返回a NSArray,您可以使用:
NSMutableArray *titles = [NSMutableArray arrayWithArray:[xpathParser search:@"//h3"]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25090 次 |
| 最近记录: |