나는 코코아/Objective-C에 대해 배우고 있으며 NSString
을 수정하고 문자열에서 개별 문자를 편집 할 수 있도록 노력하고 있습니다. 난 그것을 배열의 문자로 변경하고 그것을 다시 변경할 수있게되었지만 c[0] = 'b'
을 시도하면 "읽기 전용 변수를 할당 할 수 없습니다"라고 표시됩니다. .m 파일과 .h 파일을 포함 시켰습니다.Objective-C의 개별 문자 편집
이은하는 .m 파일입니다
import "AppDelegate.h"
@implementation AppDelegate
@synthesize textField, myLabel;
-(IBAction)changeLabel:(id)sender {
NSString *message = [[NSString alloc] initWithFormat:@"Hello, %@!", [textField stringValue]];
NSString *message2 = [[NSString alloc] initWithFormat:@"%@ This part was added on afterwards.", message];
const char *c = [message2 UTF8String];
c[0] = 'b';
NSString *output;
output = [NSString stringWithCString:c encoding:NSASCIIStringEncoding];
[myLabel setStringValue:output];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
@end
이것은 .H 파일 : 그것은 비록 내가
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *textField;
@property (weak) IBOutlet NSTextField *myLabel;
-(IBAction)changeLabel:(id)sender;
@property (weak) IBOutlet NSTextField *changeButtonText;
@end
I는 관련되지 않은 .H 파일을 가정하고 있습니다 만, 그것을 포함 시켜라.
'UTF8String' 메시지는'const char *'를 반환합니다. 한정어를 삭제하는 것은 좋은 생각이 아닙니다. – Jack
정말 고마워요. 그럼에도 불구하고 경고를받습니다. – dunnmifflsys
@ user2423270 - 작동 할 수도 있지만 Jack이 말하듯이 이것은 좋은 생각이 아닙니다. 변경하지 않아야하는 메모리에 대한 포인터가 부여되었으므로 변경하는 것이 현명합니다. – CRD