简体   繁体   中英

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. However, i have no idea what purpose this is for: self.weather <-- no clue how that is being used. Can you explain the significance of self.weather ?

[self.weather upcomingWeather];

There are no functions in Objective-c, there are messages. self.weather is an object to which you send message upcomingWeather . 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.

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.

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? 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. You want to focus on the networking part and not on navigating NSDictionary keys, right?

You are right, self.weather is dictionary, but this guy created helper categories, so NSDictionary is extended(among others) with this method:

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

So when you send upcomingWeather message, this method will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM