简体   繁体   English

ios的新功能-不确定如何使用属性

[英]new to ios - not sure how property is being used

I have been going through the following tutorial and came across this line which I dont understand how it works: 我已经阅读了以下教程 ,遇到了这一行,但我不明白它是如何工作的:

NSArray *upcomingWeather = [self.weather upcomingWeather];

I have tried to understand how this works, and spent long hours. 我试图了解它是如何工作的,并花了很长时间。 here is what i know. 这就是我所知道的。 upcomingWeather is a method which extracts a json portion and returns it into an array. futureWeather是一种提取json部分并将其返回到数组的方法。 However, i have no idea what purpose this is for: self.weather <-- no clue how that is being used. 但是,我不知道这是什么目的:self.weather <-不知道如何使用它。 Can you explain the significance of self.weather ? 您能解释一下self.weather的意义吗?

[self.weather upcomingWeather];

There are no functions in Objective-c, there are messages. Objective-c中没有功能,有消息。 self.weather is an object to which you send message upcomingWeather . self.weather是您向其发送upcomingWeather self.weather消息的对象。 These messages are something similar to function with one big difference : if the object is nil, exception is silenced and you will get no error when sending message. 这些消息与功能相似,但有一个很大的区别:如果对象为nil,则异常被静音,并且在发送消息时不会出错。

Furthermore, in any given case first part in [%@ %@] is an object, and the second one is the message you are sending. 此外,在任何给定情况下,[%@%@]中的第一部分都是对象,第二部分是您要发送的消息。 In case you have more then one parameter you will use it as : 如果您有多个参数,则可以将其用作:

[self.weather upcomingWeather:parameterOne andWithParameterTwo:parameterTwo];

The important thing to mention here is that, when declaring funtions, you have two parts : private and public. 这里要提到的重要一点是,在声明功能时,您分为两个部分:私有和公共。 You send message with accessing public part and use private part in the function. 您可以通过访问公共部分发送消息,并在功能中使用私有部分。 To better understand this take a look at the example: 为了更好地理解这一点,请看以下示例:

-(void) test:(NSString*)PO andWithParameterTwo(NSString*)PW{}

PO and PW is private parts - you use them in method, while andWithParametersTwo is public part and you access them when you send that message. POPW是私有部分-您在方法中使用它们,而andWithParametersTwo是公共部分,并且在发送该消息时可以访问它们。

Edit: 编辑:

“Wait a minute!”, you might be thinking. “等一下!”,您可能在想。 What is this [self.weather upcomingWeather]? 这是什么? If self.weather is a plain old NSDictionary, how does it know what “upcomingWeather” is? 如果self.weather是普通的NSDictionary,它怎么知道什么是“即将到来的天气”? To make it easier to display the data, I added a couple of helper categories on NSDictionary in the starter project: NSDictionary+weather NSDictionary+weather_package These categories add some handy methods that make it a little easier to access the data elements. 为了使显示数据更容易,我在启动程序项目的NSDictionary上添加了几个帮助程序类别:NSDictionary + weather NSDictionary + weather_package这些类别添加了一些方便的方法,这些方法使访问数据元素更加容易。 You want to focus on the networking part and not on navigating NSDictionary keys, right? 您想专注于网络部分而不是导航NSDictionary密钥,对吗?

You are right, self.weather is dictionary, but this guy created helper categories, so NSDictionary is extended(among others) with this method: 没错, self.weather是字典,但是此人创建了帮助者类别,因此NSDictionary使用以下方法进行了扩展:

- (NSArray *)upcomingWeather
{
    NSDictionary *dict = self[@"data"];
    return dict[@"weather"];
}

So when you send upcomingWeather message, this method will be called. 因此,当您发送upcomingWeather消息时,将调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 7中UIImageview的新tintColor属性可以用于动画图像吗? - Can the new tintColor property of UIImageview in iOS 7 be used for animating images? 如何修复mapKit Ios中使用的多余内存? - How to fix the excess memory being used in mapKit Ios ? 如何防止IBOutlet属性被释放(iOS使用ARC) - How to keep IBOutlet property from being released (iOS using ARC) iOS:何时分配以及何时创建分配给属性的对象的新副本 - iOS: When to assign & when to create new copy of objects being assigned to a property 如何确保正在显示启动屏幕 - How to make sure that launch screen is being displayed iOS,自动版式会产生高度,不确定如何? - iOS, Autolayout derives height, not sure how? iOS,NSString属性混淆为UITextField - iOS, NSString property confused as being UITextField 如何在使用我的应用程序时禁用所有 iOS 通知横幅? - How can I disable all iOS notification banners while my app is being used? 在运行我的iPhone应用程序时,如何知道正在使用的IOS? - How do I know what IOS is being used, when running my iPhone app? 如何使用不同的 <link> 头标签中的标签取决于所使用的IOS设备? - how to use a different <link> tag in the head tag depending on what IOS device is being used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM