热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SimpleStocks协议使用(Apple示例代码)-SimpleStocksprotocolusage(Applesamplecode)

IntheSimpleStockssamplecodeprovidedbyApple,dataSourceisdefinedasfollows:在Apple提供的Simple

In the SimpleStocks sample code provided by Apple, dataSource is defined as follows:

在Apple提供的SimpleStocks示例代码中,dataSource定义如下:

property (nonatomic, weak) IBOutlet id  dataSource;

and then used variously as follows (only 2 examples, but there are more in the code)

然后如下使用各种各样(只有2个例子,但代码中还有更多)

NSInteger dataCount = [self.dataSource graphViewDailyTradeInfoCount:self]; 
NSArray *sortedMOnths= [self.dataSource graphViewSortedMonths:self];

I understand what graphViewDailyTradeInfoCount does, and also graphViewSortedMonths.I would have understood: NSInteger dataCount = [APLStockViewController graphViewDailyTradeInfoCount:self]; (Tried that, and I think I know why that can't work)

我理解graphViewDailyTradeInfoCount做了什么,还有graphViewSortedMonths.I会理解:NSInteger dataCount = [APLStockViewController graphViewDailyTradeInfoCount:self]; (试过,我想我知道为什么那不起作用)

Here's the rub: The way I read the 1st line of code, dataSource is being sent the message graphViewDailyTradeInfoCount (which returns some numeric value specifying how many transactions occurred). If we assume 25 transactions, essentially [dataSource 25]; And now my mind is boggling, because I am strongly expecting that somewhere, there would be code telling dataSource what to do with the 25 it is being sent. But there isn't!!!

这是一个问题:我读取第一行代码的方式,dataSource正在发送消息graphViewDailyTradeInfoCount(返回一些指定发生了多少事务的数值)。如果我们假设25个交易,基本上是[dataSource 25];而现在我的思绪令人难以置信,因为我强烈期待某个地方,会有代码告诉dataSource如何处理正在发送的25。但是没有!!!

Then the boggling increases by an order of magnitude: dataSource having been sent the 25, it now returns that and puts it into dataCount. How does it know to do that, there's no code anywhere that I can find for that either.

然后令人难以置信的增加了一个数量级:dataSource已经发送了25,它现在返回并将其放入dataCount。怎么知道这样做,我也找不到任何代码。

And now, tickle me ballistic, in the second line of code, dataSource knows to accept (and then return) an NSArray.

而现在,在第二行代码中,让我弹跳,dataSource知道接受(然后返回)一个NSArray。

In my ever rarer sane moments, it looks to me like dataSource really doesn't do anything other than "pass through" stuff that it gets handed. Computers can't do magic, it feels like a feature, but where is it documented ?

在我罕见的理智时刻,它看起来像dataSource除了“传递”它所传递的东西之外什么都不做。计算机不能做魔术,感觉就像一个功能,但它在哪里记录?

Thanks!

谢谢!

2 个解决方案

#1


1  

Here's the rub: The way I read the 1st line of code, dataSource is being sent the message graphViewDailyTradeInfoCount (which returns some numeric value specifying how many transactions occurred).

这是一个问题:我读取第一行代码的方式,dataSource正在发送消息graphViewDailyTradeInfoCount(返回一些指定发生了多少事务的数值)。

Correct.

正确。

If we assume 25 transactions, essentially [dataSource 25];

如果我们假设25个交易,基本上是[dataSource 25];

Incorrect (based on the [] and your description, you think we're sending 25 to the data source). After the call, dataCount = 25.

不正确(基于[]和您的描述,您认为我们正在向数据源发送25)。通话结束后,dataCount = 25。

The call is asking the data source to return data to us. It is passing ourself to the data source (:self) for information purposes but the intent of the request is to get information from the data source.

该调用要求数据源将数据返回给我们。它将自己传递给数据源(:self)以供参考,但请求的目的是从数据源获取信息。

Now that we have the 25 (stored in dataCount) we can do something with it.

现在我们有了25(存储在dataCount中),我们可以用它做点什么。

Similarly, in the second line of code, the data source is just returning the array. We aren't passing it an array.

类似地,在第二行代码中,数据源只是返回数组。我们没有将它传递给数组。

For the previous mini question:

对于之前的迷你问题:

NSInteger dataCount = [APLStockViewController graphViewDailyTradeInfoCount:self];

NSInteger dataCount = [APLStockViewController graphViewDailyTradeInfoCount:self];

This can't work because APLStockViewController is a class. We need to talk to an instance of the class (which is where self.dataSource comes in).

这不起作用,因为APLStockViewController是一个类。我们需要与类的实例(self.dataSource的来源)进行对话。

#2


0  

I think you might be over complicating how delegates and datasources work. Datasource points to some instance of some object.

我认为你可能会使委托和数据源的工作方式变得复杂。数据源指向某个对象的某个实例。

When some object said it was a APLSimpleStockViewDataSource, it was saying that it implemented some methods, including graphViewDailyTradeInfoCount: and graphViewSortedMonths:. That object has SomeInstanceOfAPLSimpleStockView.datasource = self (or whatever the name of the class that you're working in was) somewhere in it's creation, like init or viewdidload

当一些对象说它是一个APLSimpleStockViewDataSource时,它说它实现了一些方法,包括graphViewDailyTradeInfoCount:和graphViewSortedMonths:。该对象具有SomeInstanceOfAPLSimpleStockView.datasource = self(或者您正在处理的类的名称),它在创建的某个位置,如init或viewdidload

It's the same as if you had some MyObject* myobj that implemented those methods, and you called [myobj graphViewSortedMonths:self

就像你有一些MyObject * myobj实现了那些方法一样,你调用了[myobj graphViewSortedMonths:self


推荐阅读
author-avatar
贺群爱你_235
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有