2015-11-15 19 views
1

我有本地sample_data.son工作代碼,但我想使用遠程sample_data.json。它的工作,但我沒有找到真正的方法。我的代碼如下。更改本地數據JSON到遠程數據JSON

- (void)generateData 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     // Add code here to do background processing 
     // 
     // 
     NSError* err = nil; 
     data = [[NSMutableArray alloc] init]; 
     companyData = [[NSMutableArray alloc] init]; 
     NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"sample_data" ofType:@"json"]; 
     NSArray* contents = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Add code here to update the UI/send notifications based on the 
      // results of the background processing 
      [contents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
       [data addObject:[NSDictionary dictionaryWithObjectsAndKeys:[[obj objectForKey:@"first_name"] stringByAppendingString:[NSString stringWithFormat:@" %@", [obj objectForKey:@"last_name"]]], @"DisplayText", [obj objectForKey:@"email"], @"DisplaySubText",obj,@"CustomObject", nil]]; 
       [companyData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[obj objectForKey:@"company_name"], @"DisplayText", [obj objectForKey:@"address"], @"DisplaySubText",obj,@"CustomObject", nil]]; 
      }]; 
     }); 
    }); 
} 

#pragma mark MPGTextField Delegate Methods 

- (NSArray *)dataForPopoverInTextField:(MPGTextField *)textField 
{ 
    if ([textField isEqual:self.name]) { 
     return data; 
    } 
    else if ([textField isEqual:self.companyName]){ 
     return companyData; 
    } 
    else{ 
     return nil; 
    } 
} 

- (BOOL)textFieldShouldSelect:(MPGTextField *)textField 
{ 
    return YES; 
} 

- (void)textField:(MPGTextField *)textField didEndEditingWithSelection:(NSDictionary *)result 
{ 
    //A selection was made - either by the user or by the textfield. Check if its a selection from the data provided or a NEW entry. 
    if ([[result objectForKey:@"CustomObject"] isKindOfClass:[NSString class]] && [[result objectForKey:@"CustomObject"] isEqualToString:@"NEW"]) { 
     //New Entry 
     [self.nameStatus setHidden:NO]; 
    } 
    else{ 
     //Selection from provided data 
     if ([textField isEqual:self.name]) { 
      [self.nameStatus setHidden:YES]; 
      [self.web setText:[[result objectForKey:@"CustomObject"] objectForKey:@"web"]]; 
      [self.email setText:[[result objectForKey:@"CustomObject"] objectForKey:@"email"]]; 
      [self.phone1 setText:[[result objectForKey:@"CustomObject"] objectForKey:@"phone1"]]; 
      [self.phone2 setText:[[result objectForKey:@"CustomObject"] objectForKey:@"phone2"]]; 
     } 
     [self.address setText:[[result objectForKey:@"CustomObject"] objectForKey:@"address"]]; 
     [self.state setText:[[result objectForKey:@"CustomObject"] objectForKey:@"state"]]; 
     [self.zip setText:[[result objectForKey:@"CustomObject"] objectForKey:@"zip"]]; 
     [self.companyName setText:[[result objectForKey:@"CustomObject"] objectForKey:@"company_name"]]; 
    } 
} 

我只需要將本地解析代碼更改爲遠程解析代碼即可。你

還可以看到當地的JSON這裏 https://github.com/gaurvw/MPGTextField

+1

建議:簡化代碼,聲明開始'[數據ADDOBJECT:'是278字符和相當難以理解。首先是不必要的代碼,數組和字典有一個字面語法,簡化了它們的創建過程,最後一些例如結合名字和姓氏的東西是中間語句的一個好候選。代碼應該是爲了易於理解而編寫給下一個開發人員,而不是編譯器。 – zaph

+0

有什麼想法嗎?仍在等待 – SwiftDeveloper

回答

1

不是一個答案,作爲格式化的回答簡潔的代碼示例原始項目。

原文:

[data addObject:[NSDictionary dictionaryWithObjectsAndKeys:[[obj objectForKey:@"first_name"] stringByAppendingString:[NSString stringWithFormat:@" %@", [obj objectForKey:@"last_name"]]], @"DisplayText", [obj objectForKey:@"email"], @"DisplaySubText",obj,@"CustomObject", nil]]; 

重新格式化:

NSString *fullName = [NSString stringWithFormat:@"%@ %@", obj[@"first_name"], obj[@"last_name"]]; 
[data addObject: @{ 
        @"DisplayText" : fullName, 
        @"DisplaySubText" : obj[@"email"], 
        @"CustomObject" : obj 
        }]; 
+0

耶謝謝:)但仍在等待 – SwiftDeveloper