I originally learned Swift. Now, I am trying to learn Objective-C, but I am having some difficulty. They seem a lot more different than I originally thought. Here, I am simply trying to display "Hello World". This is in the .m file.
- (void)viewDidLoad {
[super viewDidLoad];
name = *"World";
NSString word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@, %@", word, name];
}
In my .h file I have:
@interface ViewController : UIViewController {
NSString *name;
}
In Objective-C variables are declared VariableType variableName = Value;
for example
NSInteger number = 10;
If VariableType
is an object like NSString
you have to add an asterisk and unlike Swift a literal string must start with a leading @
.
NSString *name = @"World";
NSString *word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@ %@", word, name];
You are encouraged to read Apple's language guide Working with Objective-C
Yeah, Objective-C has some extra symbol stuff going on when compared to Swift. I recommend reading or watching some tutorials on the basics. Also, repetition always helps when learning. The code below should work for you. Be sure to pay attention to the details.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
name = @"World";
NSString *word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@ %@", word, name];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.