简体   繁体   中英

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

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

In fact it doesn't return NSString object but NSArray object. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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