2013-12-23 44 views
0

我正在從Web服務響應,響應基於逗號分隔的URl如何在IOS格式化字符串(@ 「URL」)

NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     NSLog(@"ResponseData: %@", str); 

,其輸出是一樣

ResponseData: http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-00-12-46.jpg,http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-01-12-32.jpg 

我想通過我的圖象畫廊的響應這需要像下面的URL格式

networkImages = [[NSArray alloc] initWithObjects:@"http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-00-12-46.jpg",@"http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-01-12-32.jpg",nil]; 

我如何格式化我到響應這樣我可以將其存儲到一些變量E,G; ABC和傳遞到

networkImages = [[NSArray alloc] initWithObjects:abc,nil]; 

回答

0

NSString有一個方法,其componentsSeparatedByString:不正是此。

networkImages = [str componentsSeparatedByString:@","]; 
+1

這應該工作,除非您在其中一個網址中有逗號。如果可以,我會使用不是有效的URL字符的分隔符。或者返回一些JSON或XML格式的數據。 – DrummerB

+0

實例方法'-componenetsSeparatedByStrings:'找不到(返回類型默認爲'id')當我試過這個 – Exception

+0

@QuickApp檢查組件的拼寫時,可以從答案複製/粘貼方法名 – thelaws

0

我覺得你有兩種方式:

第一種方式; (我已經在我的新聞應用中使用) *您必須使用JSON結果。 *如果你願意,你可以使用SBJsonParserSDWebImage

NSError *myError = nil; 
NSString *link = [NSString stringWithFormat:@"http://www.myexamplenews.com/mobil-app.php?q=imageList&id=%i",galleryId]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:0 timeoutInterval:5]; 
NSURLResponse *response=nil; 
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&myError]; 
NSString *jsonString = [[NSString alloc] initWithData:data encoding:nil]; 
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];  
if (myError == nil) 
{ 
    NSArray *jsonGalleryDetail = [jsonParser objectWithString:jsonString]; 
    jsonParser = nil; 
    GalleryDetailView *view = [[GalleryDetailView alloc] initWithFrame:[[UIScreen mainScreen] andImageList: jsonGalleryDetail]; 
    [self.navigationController pushViewController:GalleryDetailView animated:YES]; 
} 

GalleryDetailView.h

@interface GalleryDetailView : UIView 
{ 
} 
- (id)initWithFrame:(CGRect)frame andImageList:(NSArray *)myList; 
@end 

GalleryDetailView.m

@implementation GalleryDetailView 

- (id)initWithFrame:(CGRect)frame andImageList:(NSArray *) myList; 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
    //Your code here 
    for (NSString *str in myList) 
    { 
      NSURL *imageURL = [NSURL URLWithString:str]; 
      //This is SDWebImage extension. 
      UIImage *image = [UIImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
    } 
    } 
    return self; 
} 
@end 

方式二;

NSArray *arr = [ResponseData componentsSeparatedByString:@","]; 
GalleryDetailView *view = [[GalleryDetailView alloc] initWithFrame:[[UIScreen mainScreen] andImageList: arr]; 
    [self.navigationController pushViewController:GalleryDetailView animated:YES]; 

我更喜歡使用JSON數據。它是由你決定。我希望這些信息對你有所幫助。