2014-03-30 29 views
0

NSDictionary讓我困惑。它有點像我收集的結構和數組的混合體。我正在瀏覽iOS編程書,它讓我們把一個JSON對象放入一個NSDictionary。對於具有「標題」鍵的對象,所有返回都是正確的,但對於upcoming類,在upcoming內有變量。在我的腦海中,這是如何構建NSDictionary我如何訪問NSDictionary的另一個成員內的成員,同時保持成員之間的鏈接

struct classes { 
    NSString *title; 
    struct upcoming[] { 
     NSString *end_date; 
     NSString *start_date; 
     NSString *instructor; 
     } 
    } 
} 

我一直試圖「通過使用變化的for循環內S:

NSLog(@"%@", [course objectForKey:@"upcoming.start_date"]); 

的我這樣做的目的是要追加的title字符串」檢索start_date s表示在upcoming類別中的值爲start_date。喜歡的東西...

NSString *value = course[@"title"]; 
if ([course [email protected]"upcoming"] [value stringByAppendingString: 
             @" course[@"upcoming.start_date"]; 
NSLog(@"%@", value); 

怎樣讓這個如果沒有爲upcoming一個或多個值,那麼它會通過將字符串顯示的要不然也不會做任何顯示的起始日期值?顯然upcoming.start_date是不是做到這一點!和start_date本身沒有鏈接到相關title。我需要它們都被鏈接來追加適當的字符串。

解決方案:

NSString *title = [course objectForKey:@"title"]; 
NSArray *upcomingCourses = [course objectForKey:@"upcoming"]; 
if (upcomingCourses.count > 0 
    NSString *showDate = [NSString stringWithFormat:@"%@. Next Class Date: %@" 
    title, [[upcomingCourses objectAtIndex:0] objectForKey:@"start_date"]]; 
    NSLog(@"%@", showDate); 
} 
else NSLog(@"%@", title); 

發佈NSDictionaryNSLog

courses =  (
      { 
     title = "Advanced iOS Bootcamp"; 
     upcoming =    (
          { 
       "end_date" = "2014-04-04"; 
       instructors = Blocksom; 
       location = "BNR West, California"; 
       "start_date" = "2014-03-31"; 
      }, 
          { 
       "end_date" = "2014-05-16"; 
       instructors = Dalrymple; 
       location = "BNR West, California"; 
       "start_date" = "2014-05-12"; 
      }, 
          { 
       "end_date" = "2014-06-20"; 
       instructors = Ward; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-06-16"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/advanced-ios-bootcamp"; 
    }, 
      { 
     title = "Beginning iOS (iPhone/iPad)"; 
     upcoming =    (
          { 
       "end_date" = "2014-04-18"; 
       instructors = Mathias; 
       location = "BNR West, California"; 
       "start_date" = "2014-04-12"; 
      }, 
          { 
       "end_date" = "2014-05-02"; 
       instructors = "Keur, Ward"; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-04-26"; 
      }, 
          { 
       "end_date" = "2014-05-23"; 
       instructors = Christopher; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-05-17"; 
      }, 
          { 
       "end_date" = "2014-06-06"; 
       instructors = Christopher; 
       location = "BNR West, California"; 
       "start_date" = "2014-05-31"; 
      }, 
          { 
       "end_date" = "2014-07-25"; 
       instructors = Ritchie; 
       location = "BNR West, California"; 
       "start_date" = "2014-07-19"; 
      }, 
          { 
       "end_date" = "2014-08-01"; 
       instructors = "Usiyan, Keur"; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-07-26"; 
      }, 
          { 
       "end_date" = "2014-09-19"; 
       instructors = Ritchie; 
       location = "BNR West, California"; 
       "start_date" = "2014-09-13"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/beginning-ios"; 
    }, 
      { 
     title = "Mobile Design Bootcamp"; 
     upcoming =    (
          { 
       "end_date" = "2014-06-13"; 
       instructors = "Harper, Porter"; 
       location = "BNR West, California"; 
       "start_date" = "2014-06-09"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/mobile-design-bootcamp"; 
    }, 
      { 
     title = "Python Programming"; 
     upcoming =    (
          { 
       "end_date" = "2014-04-24"; 
       instructors = Cassell; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-04-21"; 
      }, 
          { 
       "end_date" = "2014-06-19"; 
       instructors = Cassell; 
       location = "BNR West, California"; 
       "start_date" = "2014-06-16"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/python-programming"; 
    }, 
      { 
     title = "Beginning iOS Game Development"; 
     upcoming =    (
          { 
       "end_date" = "2014-06-20"; 
       instructors = Strougo; 
       location = "BNR West, California"; 
       "start_date" = "2014-06-16"; 
      }, 
          { 
       "end_date" = "2014-09-26"; 
       instructors = Strougo; 
       location = "BNR West, California"; 
       "start_date" = "2014-09-22"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/beginning-ios-game-development"; 
    }, 
      { 
     title = "Android Bootcamp"; 
     upcoming =    (
          { 
       "end_date" = "2014-05-16"; 
       instructors = Phillips; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-05-12"; 
      }, 
          { 
       "end_date" = "2014-06-20"; 
       instructors = Marsicano; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-06-16"; 
      }, 
          { 
       "end_date" = "2014-07-11"; 
       instructors = Stewart; 
       location = "BNR West, California"; 
       "start_date" = "2014-07-07"; 
      }, 
          { 
       "end_date" = "2014-10-03"; 
       instructors = Phillips; 
       location = "BNR West, California"; 
       "start_date" = "2014-09-29"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/android-bootcamp"; 
    }, 
      { 
     title = "HTML5 Apps with jQuery"; 
     upcoming =    (
          { 
       "end_date" = "2014-05-16"; 
       instructors = Aquino; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-05-12"; 
      }, 
          { 
       "end_date" = "2014-06-13"; 
       instructors = Gandee; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-06-09"; 
      }, 
          { 
       "end_date" = "2014-08-01"; 
       instructors = Aquino; 
       location = "BNR West, California"; 
       "start_date" = "2014-07-28"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/html5-apps-with-jquery"; 
    }, 
      { 
     title = "Ruby on the Server"; 
     upcoming =    (
          { 
       "end_date" = "2014-06-13"; 
       instructors = Stewart; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-06-09"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/ruby-on-the-server"; 
    }, 
      { 
     title = "iOS Bootcamp - Fast Track"; 
     upcoming =    (
          { 
       "end_date" = "2014-07-11"; 
       instructors = Usiyan; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-07-07"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/ios-bootcamp-fast-track"; 
    }, 
      { 
     title = "Cocoa I Bootcamp"; 
     upcoming =    (
          { 
       "end_date" = "2014-07-25"; 
       instructors = Preble; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-07-21"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/cocoa-i-bootcamp"; 
    }, 
      { 
     title = "OpenGL ES Bootcamp"; 
     upcoming =    (
          { 
       "end_date" = "2014-07-25"; 
       instructors = Blocksom; 
       location = "Atlanta, Georgia"; 
       "start_date" = "2014-07-21"; 
      } 
     ); 
     url = "https://training.bignerdranch.com/classes/opengl-es-bootcamp"; 
    } 
); 

}

+3

一個'NSDictionary'是鍵的集(無重複)和值的集合之間的映射。根據您熟悉的其他語言,其結構可能被稱爲「[關聯數組](http://en.wikipedia.org/wiki/Associative_array)」,「哈希表」或「映射」。每個鍵對應一個值;他們是配對的,並且這些配對沒有訂購。 –

+0

你可以把你的字典的實際結構或佈局納入問題嗎?通過NSLog()打印出來就可以了。 –

+0

@JoshCaswell更新。它的長! – Scott

回答

3

NSDictionary中,您可以通過鑰匙檢索對象。關鍵是另一個對象,但通常使用NSString(限制是鍵必須是唯一的,值不需要)。任何對象都可以存儲在密鑰下,所以在你的情況下即將到來的是一個NSDictionaries數組。所以,你得到

dictionary { 
    title: someString 
    upcoming: array { 
     0: dictionary { 
      end_date: somestring 
      start_date: someString 
      instructor: someString 
     } 
     1: dictionary { 
      end_date: somestring 
      start_date: someString 
      instructor: someString 
     } 
    } 

} 

注意,上面沒有任何特定的語言或語法,在你的數據的「結構」的只是一種表象。

要檢索課程詳細信息,您可以像訪問其他任何人一樣訪問「即將到來的」對象,並將其分配給變量。

假設course是根NSDictionary

NSString *title=(NSString *)[course objectForKey:@"title"]; 
NSArray *upcomingCourses=(NSArray *)[course objectForKey:@"upcoming"]; 

NSMutableString *startDates=[[NSMutablestring alloc]init]; 

for (int i=0;i<[upcomingCourses count];i++) 
{ 
    NSDictionary *upcomingCourse = (NSDictionary *)[upcomingCourses objectAtIndex:i]; 
    NSString *startDate = (NSString *)[upcomingCourse objectForKey:@"start_date"]; 
    NSString *endDate = (NSString *)[upcomingCourse objectForKey:@"end_date"]; 
    NSString *instructor = (NSString *)[upcomingCourse objectForKey:@"instructor"]; 

    [startDates appendFormat:@"%@, ",startDate]; 
} 
+0

你能告訴我一些粗略的語法來說明如何將它添加到''for''循環中嗎?也許我需要將對象添加到數組來鏈接它們? – Scott

+0

對不起,我錯過了即將到來的是一個數組 - 讓我修改我的答案 – Paulw11

+0

嗯,我想我需要回去學習NSDictionary了。我根本沒有處理這個。我仍然期待看到像'if(objectKey @「title」hasNonNullObject:objectKey @「即將到來的)doSomething();''我無法想象這將如何連接字符串。 – Scott