2012-07-28 61 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上提问。