2012-07-28 52 views
-1

好吧我正在學習數組以及如何像以前一樣使用它們......(用於做很多腳本,但現在正在嘗試學習開發ipad和iphone應用程序的Objective-C數組學習

但我的問題是,我有它它就會從雅虎財經與for循環一組數據..

但現在我的問題是,我怎麼能與陣列的數據只是一個peice的已工作拉

這裏是我的榜樣

-(IBAction) clicked:(id)sender { 

    NSString * StockOneYahooFinance = [NSString stringWithFormat:@"http://finance.yahoo.com/q/hp?s=S+Historical+Prices"]; 
    NSString * PulledStockOne = [NSString stringWithContentsOfURL:[NSURL URLWithString:StockOneYahooFinance] encoding:1 error:nil]; 

    for (i=1;i<=10;i++){ 

     NSString *StartPulling = [[PulledStockOne componentsSeparatedByString:@"nowrap align="] objectAtIndex:i]; 

     NSString *StartOpen = [[StartPulling componentsSeparatedByString:@">"] objectAtIndex:3]; 
     NSString *Open = [[StartOpen componentsSeparatedByString:@"<"] objectAtIndex:0]; 

     NSString *StartClose = [[StartPulling componentsSeparatedByString:@">"] objectAtIndex:9]; 
     NSString *Close = [[StartClose componentsSeparatedByString:@"<"] objectAtIndex:0]; 

     NSMutableArray *StockOpens = [[NSMutableArray alloc] initWithCapacity:6]; 
     [StockOpens addObject:Open]; 


     sixtyday.text = [OpenValues objectAtIndex:10]; 
     nintyday.text = [CloseValues objectAtIndex:10]; 


     if ([OpenValues objectAtIndex:10]=[OpenValues objectAtIndex:11] { 
     sevenday.text = @"Plus One"; 
     } 
    } 
} 

但現在我要像做

year.text=StockOpens[5]; 

我怎樣才能做到這一點。

回答

1

從Xcode 4.4(LLVM 4.0)開始,文字可用於Objective-C中的C風格下標。

year.text = StockOpens[5]; 

LLVM有記載利用文字在這裏:Objective-C Literals

注:基礎框架,因爲鏘將翻譯文字使用,在這種情況下objectAtIndexedSubscript:,在OS X v10.8(或iOS 6)是必須的。

+0

字典鍵有類似的東西嗎?太好了! – 2012-07-28 17:32:23

+0

是的!我通過鏈接到LLVM的文檔更新了我的解決方案。 – jtomschroeder 2012-07-28 17:36:11

+1

請注意,這也需要使用iOS 6或OS X 10.8 SDK附帶的Foundation版本。 – 2012-07-28 18:07:06

1

StockOpens是一個數組對象,因此您需要調用一個方法來獲取索引處的對象。在NSMutableArray中的[StockOpens ObjectAtIndex:5]

year.text = [[StockOpens objectAtIndex:5]StringValue]; 

要做到StockOpens [5]你需要使用C-陣列。

+1

只是一個放大:@ user1544586:如果你習慣於從腳本語言中支持對數組的訪問,它在Obj-C中的工作方式不同。你也不能在數組中存儲原始值(int,float);他們需要轉換爲基於NSObject的類之一(NSNUmber,NSValue等)。 – 2012-07-28 17:10:42

+0

@skinnyTOD在Objective-C 2.5中,您現在可以使用'objectAtSubscriptedIndex'。 – 2012-07-28 17:27:01

+0

@ RichardJ.RossIII - 首先我聽說過。 iOS 6支持嗎? – 2012-07-28 17:31:53

0

這要看是什麼樣/類對象的填寫StockOpens用,如果它只是NSString的,你可以做

year.text = [StockOpens objectAtIndex:5]; 

如果是一些其他的對象不是一個字符串,你也許可以調用其描述:

year.text = [[StockOpens objectAtIndex:5] description]; 

P. s .:有關於developer.apple.com的文檔,請閱讀它!這個問題非常簡單(而且很基本),所以不能在SO上提問。