Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface MyObject ()
- //This is the interface
- //Properties and methods are defined here
- //This is usually kept in a header file
- //Header files are what Xcode uses for code completion lookup.
- //They are not strictly required
- //This is what a property looks like
- @property (strong, nonatomic) NSString *myString;
- //A couple things worth noting there:
- //1. Notice that when we define the variable it is a pointer.
- //NSString is a datatype provided with the Foundation Kit.
- //Think of that as ObjC's base classes. You could have ObjC without
- //Foundation but you'll never see that in the wild.
- //All data types in ObjC are objects. Because objects are not C primitives
- //but, rather, complex structures we need a pointer to them rather than
- //an absolute reference. If you are going "Ah! Pointers! No!" don't worry,
- //you don't see them at all except when declaring a variable. When you
- //reference it you'd just use myString or whatever.
- //2. That (strong, nonatomic) stuff? That's ARC (automatic reference
- //counting) keywords. Those keywords help the ARC system manage memory
- //for you (gross oversimplification.) Don't worry about it. You can
- //learn more later.
- //3. Notice the "@" before "property?" The "@" symbol tells the compiler
- //"Hey, what's coming next is some weird ObjectiveC shit, so don't interpret
- //it as C." ObjC breaks out the @ whenever it does something ObjC-ish. You'll
- //see it a lot. Notice it before interface and end too, to tell the compiler
- //"Hey, this is ObjC stuff."
- - (void) doSomething;
- //This is a method declaration. You should understand this easily.
- //We're declaring a method called doSomething that returns nothing.
- //The - means it is an instance method. If it were a + it would be
- //a class method and you could call it on uninstantiated classes.
- @end
- @implementation MyObject
- @synthesize myString
- //What's this? Well, it's got a @ before it so we know it is weird
- //ObjC stuff. What's it do? Well, that's our property...
- //The synthesize directive auto-generates your getters and setters.
- //So instead of implementing some stupid setMyString or getMyString
- //the @synthesize directive does it for you for any property you've declared
- //Let me guess, you just said "Woah, that's awesome!" Shit yeah it is.
- //And here's the meat of our method
- - (void) doSomething {
- [self setMyString: @"Yeah Booooiii"];
- //This is the only part of ObjC that's really all that weird
- //and, it's quite simple once you get used to it.
- //The brackets enclosing this directive let us know we are
- //sending a message to an object. You can read that as
- //we're calling a method on an object. For now "send a message" == "call a method"
- //in your brain. There are subtle differences, but they don't mean anything
- //right now.
- //So self is obvious. We're referring to ourself, like this in JS.
- //What are we doing? We're calling setMyString: or, the setter that
- //was generated by the @synthesize directive. After that we see the value
- //we are sending. The @ before the value tells us that this is an NSString!
- //Simple!
- //in Java or JS or whatever that would simply look like:
- //self.setMyString("Yeah Boooiii");
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment