复制和QUOT;的&QUOT使用; property属性保持一个不变的NSString [英] Usage of the "copy" property attribute to maintain an immutable NSString

查看:149
本文介绍了复制和QUOT;的&QUOT使用; property属性保持一个不变的NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新到iOS开发和编程在Objective-C。我一直在做的应用开发库的练习。

这是我想了解当前的锻炼。
3.测试,如果你设置一个可变的字符串作为人的名字会发生什么,然后调用修改后的sayHello方法之前发生变异的字符串。再次添加副本属性和测试更改的NSString属性声明。

我尝试但要做到这一点,我修改,其实变化的确尽管使用拷贝财产属性的NSString。

下面是我的声明和实现,以及我的测试code。

  XYZPerson.h
#进口<基金会/ Foundation.h>@interface XYZPerson:NSObject的@属性(复制)的NSString *的firstName;
@财产的NSString * lastName的;
@property的NSDate *出生日期; - (无效)的sayHello;
- (无效)saySomething:(* NSString的)的问候;+(ID)初始化;
+(ID)personWithFirstName:(* NSString的)名字姓氏:(* NSString的)姓氏出生日期:(NSDate的*)出生日期;
@结束//XYZPerson.m
#进口XYZPerson.h@implementation XYZPerson@合成的firstName = _FirstName;
@合成的lastName = _lastName;
@synthesize DOB = _dob;
- (无效){的sayHello
    [个体经营saySomething:@!Hello World的]。
    的NSLog(@这是%@%@,self.firstName,self.lastName);
} - (无效)saySomething:(* NSString的){问候
    的NSLog(@%@,贺卡);
}+(ID)初始化{
    返回[自我personWithFirstName:@约里克名字:@鲁滨逊出生日期:1990年8月23日]
}+(ID)personWithFirstName:(* NSString的)名字姓氏:(* NSString的)姓氏出生日期:(NSDate的*){DATEOFBIRTH
    XYZPerson *人= [[自我的alloc]初始化];
    person.firstName =名字;
    person.lastName = lastName的;
    person.dob = DATEOFBIRTH;    返回的人;
}@结束//测试code
#进口<的UIKit / UIKit.h>
#进口AppDelegate.h
#进口XYZPerson.h
#进口XYZShoutingPerson.h
INT主(INT ARGC,CHAR *的argv [])
{
    @autoreleasepool {
        XYZPerson *家伙= [XYZPerson的init];
        [家伙的sayHello]        //我认为这种变化绝不会进行,但每次我运行code。
        guy.firstName = @欧阳
        [家伙的sayHello]        XYZShoutingPerson *女孩= [XYZShoutingPerson的init];
        [女孩的sayHello]
        返回UIApplicationMain(ARGC,ARGV,零,NSStringFromClass([AppDelegate类]));
    }
}


解决方案

考虑这个短的例子(运行中的 codeRunner BTW):

 #进口<基金会/ Foundation.h>@interface人:NSObject的
@属性(非原子,强)的NSString *名称; //强应该是复制
@结束@implementation人
@结束INT主(INT ARGC,CHAR *的argv []){
    @autoreleasepool {
        人* p = [人新]        *的NSMutableString名= [[的NSMutableString页头] initWithString:@爱丽丝];
        p.name =名称;
        的NSLog(@%@,p.name); //打印爱丽丝        [名appendString:@XXX];
        的NSLog(@%@,p.name); //输出Alicexxx
    }
}

我指着名称为可变的字符串,然后附加一些字符。其结果是,姓名已在对象内部改变。但是,如果你声明属性时与副本替换强,一个新的不可改变字符串将只为Person对象创建的。

这个故事的寓意是,使用复制prevents副作用,当有人传递一个对象,然后该对象的变化。

消息 - [的NSString复制] 当一个可变字符串传递(的NSMutableString),或者当它是不变的保留(的NSString)导致的副本。因此,总是宣称的NSString属性时复制:

  @属性(非原子,副本)的NSString *串; // 好
@属性(非原子,强)的NSString *串; //强应该是复制

I am very new to iOS development and programming in Objective-C. I have been doing the exercises on the app dev library.

This is the current exercise that I am trying to understand. 3. Test what happens if you set a mutable string as the person’s first name, then mutate that string before calling your modified sayHello method. Change the NSString property declarations by adding the copy attribute and test again.

I attempt to do this however, the NSString that I modify does in fact change despite the use of the copy property attribute.

Here are my declarations and implementations as well as my test code.

XYZPerson.h
#import <Foundation/Foundation.h>

@interface XYZPerson : NSObject

@property (copy) NSString *firstName;
@property NSString *lastName;
@property NSDate *dob;

- (void)sayHello;
- (void)saySomething:(NSString *)greeting;

+ (id)init;
+ (id)personWithFirstName:(NSString *)firstName lastName:(NSString *)lastName dob:(NSDate   *)dateOfBirth;


@end

//XYZPerson.m
#import "XYZPerson.h"

@implementation XYZPerson

@synthesize firstName = _firstName;
@synthesize lastName = _lastName;
@synthesize dob = _dob;


- (void)sayHello {
    [self saySomething:@"Hello World!"];
    NSLog(@"This is %@ %@", self.firstName, self.lastName);
}

- (void)saySomething:(NSString *)greeting {
    NSLog(@"%@", greeting);
}

+ (id)init {
    return [self personWithFirstName:@"Yorick" lastName:@"Robinson" dob:8/23/1990];
}

+ (id)personWithFirstName:(NSString *)firstName lastName:(NSString *)lastName dob:(NSDate   *)dateOfBirth{
    XYZPerson *person = [[self alloc] init];
    person.firstName = firstName;
    person.lastName = lastName;
    person.dob = dateOfBirth;

    return person;
}

@end

//Test code
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "XYZPerson.h"
#import "XYZShoutingPerson.h"


int main(int argc, char *argv[])
{
    @autoreleasepool {
        XYZPerson *guy = [XYZPerson init];
        [guy sayHello];

        //I thought that this change would never be made, but it is everytime I run the code.
        guy.firstName = @"Darryl";
        [guy sayHello];

        XYZShoutingPerson *girl = [XYZShoutingPerson init];
        [girl sayHello];
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

解决方案

Consider this shorter example (which runs in CodeRunner btw):

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic,strong) NSString *name; // strong should be copy
@end

@implementation Person
@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        Person *p = [Person new];

        NSMutableString *name = [[NSMutableString alloc] initWithString:@"Alice"];
        p.name = name;
        NSLog(@"%@",p.name); // prints Alice

        [name appendString:@"xxx"];
        NSLog(@"%@",p.name); // prints Alicexxx
    }
}

I'm pointing the name to a mutable string, then appending some characters. As a result, the name has changed inside the object. However, if you replace strong with copy when declaring the property, a new immutable string will be created just for the Person object.

The moral of the story is, using copy prevents side effects when someone passes an object and then that object changes.

The message -[NSString copy] results in a copy when a mutable string is passed (NSMutableString) or retain when it is immutable (NSString). Therefore, always copy when declaring NSString properties:

@property (nonatomic,copy)   NSString *string;  // OK
@property (nonatomic,strong) NSString *string;  // strong should be copy 

这篇关于复制和QUOT;的&QUOT使用; property属性保持一个不变的NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆