开发者

setObject:forKey: method of NSMutableDictionary

开发者 https://www.devze.com 2023-01-09 04:17 出处:网络
Is the first parameter supposed to be an object? A tutorial I\'m following has the first parameter being textFieldBeingEdited.text, where it is defined in the .h file as

Is the first parameter supposed to be an object? A tutorial I'm following has the first parameter being textFieldBeingEdited.text, where it is defined in the .h file as

UITextField *textFieldBeingEdited

Isn't textFieldBeingEdited an object, and text is a property of that object?

The following code crashes:

[tempValues setObject:textFieldBeingEdited forKey:tagAsNum];

If I change it to the following then it doesn't crash:

[tempValues setObject:textFieldBeingEdited.text forKey:tagAsNum];

That doesn't mak开发者_如何学Ce sense though since the first argument is supposed to be an object, and not a property.


textFieldBeingEdited.text is a property of UITextField, but it is also an object, of type NSString.


A property is syntactic sugar for a getter method that returns an object and optionally a setter method that takes an object. The text property of the UITextField object provides a getter method that returns an NSString object that can be stored in an NSDictionary.

Essentially, a property provides two methods. For example, the methods implemented/synthesised by the text property may look like this (simplified for the sake of the example):

- (NSString *) text
{
    return text;
}

- (void) setText:(NSString *) newText
{
    if (text != newText)
    {
        [text release];
        text = [newText copy];
    }
}

When you use object.text = @"Hello", it will actually send the setText: message with @"Hello" as the argument, and when you use NSString *value = object.text; it will actually send the text message, which returns an NSString object.

0

精彩评论

暂无评论...
验证码 换一张
取 消