Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么只有在使用mutableCopy时,NSMutableAttributedString addAttribute才能工作_Ios_Objective C_Nsstring_Nsattributedstring_Nsmutablecopying - Fatal编程技术网

Ios 为什么只有在使用mutableCopy时,NSMutableAttributedString addAttribute才能工作

Ios 为什么只有在使用mutableCopy时,NSMutableAttributedString addAttribute才能工作,ios,objective-c,nsstring,nsattributedstring,nsmutablecopying,Ios,Objective C,Nsstring,Nsattributedstring,Nsmutablecopying,我有以下代码: NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"]; NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"]; [boldS addAttribute:NSFontAttributeName

我有以下代码:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

[boldS addAttribute:NSFontAttributeName value:SOMEBOLDFONT range:NSMakeRange(0, boldS.length)];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
如您所见,我想加粗
部分。如果我做了上述陈述,这不起作用,但只有当我做了以下事情时,这才起作用:

[[attrS mutableCopy] replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
                         withAttributedString:boldS];

原因可能是什么?

addAttribute
无论您是否使用
mutableCopy
都有效。你的问题基于错误的假设。因此,它没有答案

运行以下命令:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];

NSMutableAttributedString *attrSCopy = [attrS mutableCopy];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");

它将输出
相等的
。注释掉
attrS
attroscopy
replaceCharactersRange:
,它将输出
不同的
addAttribute
工作,无论您是否使用
mutableCopy
。你的问题基于错误的假设。因此,它没有答案

运行以下命令:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];

NSMutableAttributedString *attrSCopy = [attrS mutableCopy];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");

它将输出
相等的
。注释掉
attrS
attroscopy
replaceCharactersRange:
,它将输出
不同的
addAttribute
工作,无论您是否使用
mutableCopy
。你的问题基于错误的假设。因此,它没有答案

运行以下命令:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];

NSMutableAttributedString *attrSCopy = [attrS mutableCopy];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");

它将输出
相等的
。注释掉
attrS
attroscopy
replaceCharactersRange:
,它将输出
不同的
addAttribute
工作,无论您是否使用
mutableCopy
。你的问题基于错误的假设。因此,它没有答案

运行以下命令:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];

NSMutableAttributedString *attrSCopy = [attrS mutableCopy];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");


它将输出
相等的
。注释掉
attrS
attrSCopy
replaceCharactersRange:
,它将输出
不同的

它是如何工作的?你在哪里调用
addAttribute
?我看不出你在哪里加粗
NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个
addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,
mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用
addAttribute
?我看不出你在哪里加粗
NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个
addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,
mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用
addAttribute
?我看不出你在哪里加粗
NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个
addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,
mutableCopy
创建了一个接收者的副本,即一个与接收者具有相同属性的新对象,它是可变的。它怎么不起作用?你在哪里调用
addAttribute
?我看不出你在哪里加粗
NSMutableAttributedString
:你所做的只是为同一个字符串更改一个字符串。是否有一个
addAttributes
或类似的东西你忘了在这里发布?我不能复制这个。这是你运行过并遇到问题的真实代码吗?和Chuck一样,我运行了这段代码,它工作得非常完美。你在其他地方犯了一个错误。凭直觉,
mutableCopy
创建了一个接收者的副本,也就是说,一个与接收者具有相同属性的新对象,它是可变的。