简体   繁体   English

从目标c中的字符串数组获取简单值

[英]get simple value from string array in objective c

My code is like... 我的代码就像...

NSString *str=[arrInitiatives valueForKey:@"FileName"];
NSLog(@"file name ----> %@",str);

 NSString *imageUrl = [NSString stringWithFormat:@"%@%@", PUBLIC_UTILITY_FORMS_URL,str];
 NSLog(@"----> %@",imageUrl);
[_image_imageview setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"noimage.png"]];
//http://setupolice.org/brcsetu/UploadFile/Lighthouse4908.jpg

from this code,i will get this 从这个代码,我会得到这个

file name ----> (
    "Lighthouse4908.jpg"
)

 ----> http://setupolice.org/brcsetu/UploadFile/(
    "Lighthouse4908.jpg"
)

I want this   

----> http://setupolice.org/brcsetu/UploadFile/Lighthouse4908.jpg

Try this 尝试这个

NSString *str=[[arrInitiatives valueForKey:@"FileName"] objectAtIndex:0];

Regards, 问候,

Amit 阿米特

When you create the str variable with code 使用代码创建str变量时

NSString *str=[arrInitiatives valueForKey:@"FileName"];

In fact it doesn't return NSString object but NSArray object. 实际上,它不返回NSString对象,而是返回NSArray对象。 That's why filename is inside parenthesis in log: 这就是文件名在日志中括号内的原因:

file name ----> (
"Lighthouse4908.jpg"
)

If you're sure that filename will always be present in the array you can try what @Amit Kalghatgi suggested in his answer. 如果您确定文件名将始终存在于数组中,则可以尝试@Amit Kalghatgi在其答案中建议的内容。

The problem is that when this array is empty you will get an error during execution of this code. 问题是,当此数组为空时,在执行此代码期间会出现错误。 I would rather do something like this: 我宁愿做这样的事情:

NSArray *filenames = [arrInitiatives valueForKey:@"FileName"];
NSString *str = nil;
if (filenames.count) {
    str =[arrInitiatives valueForKey:@"FileName"];
}
NSLog(@"file name ----> %@",str);

Of course before creating imageUrl you'll have to check if str is nil or not. 当然,在创建imageUrl之前,您必须检查str是否为nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM