ios – 使用“copy”属性属性来维护不可变的NSString

ios – 使用“copy”属性属性来维护不可变的NSString,第1张

概述我是Objective-C中iOS开发和编程的新手.我一直在app dev库上做练习. 这是我想要了解的当前练习. 3.测试如果将可变字符串设置为该人的名字会发生什么,然后在调用修改后的sayHello方法之前改变该字符串.通过添加copy属性更改NSString属性声明并再次测试. 我尝试这样做,但是我修改的NSString实际上改变了,尽管使用了copy属性. 这是我的声明和实现以及我的测试代 我是Objective-C中iOS开发和编程的新手.我一直在app dev库上做练习.

这是我想要了解的当前练习.
3.测试如果将可变字符串设置为该人的名字会发生什么,然后在调用修改后的sayHello方法之前改变该字符串.通过添加copy属性更改Nsstring属性声明并再次测试.

我尝试这样做,但是我修改的Nsstring实际上改变了,尽管使用了copy属性.

这是我的声明和实现以及我的测试代码.

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]));    }}
解决方法 考虑这个较短的例子(运行在 CodeRunner btw):
#import <Foundation/Foundation.h>@interface Person : NSObject@property (nonatomic,strong) Nsstring *name; // strong should be copy@end@implementation Person@endint 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    }}

我将名称指向一个可变字符串,然后附加一些字符.结果,名称在对象内部发生了变化.但是,如果在声明属性时将strong替换为copy,则将仅为Person对象创建新的不可变字符串.

故事的寓意是,当有人传递一个物体然后该物体发生变化时,使用副本可以防止副作用.

消息 – [Nsstring copy]在传递可变字符串(NSMutableString)时生成副本,或者在不可变(Nsstring)时保留.因此,在声明Nsstring属性时始终复制:

@property (nonatomic,copy)   Nsstring *string;  // OK@property (nonatomic,strong) Nsstring *string;  // strong should be copy
总结

以上是内存溢出为你收集整理的ios – 使用“copy”属性属性来维护不可变的NSString全部内容,希望文章能够帮你解决ios – 使用“copy”属性属性来维护不可变的NSString所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1106995.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存