Guest User

Untitled

a guest
Aug 30th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @interface MyObject ()
  2.  
  3. //This is the interface
  4. //Properties and methods are defined here
  5. //This is usually kept in a header file
  6. //Header files are what Xcode uses for code completion lookup.
  7. //They are not strictly required
  8.  
  9. //This is what a property looks like
  10. @property (strong, nonatomic) NSString *myString;
  11.  
  12. //A couple things worth noting there:
  13. //1. Notice that when we define the variable it is a pointer.
  14. //NSString is a datatype provided with the Foundation Kit.
  15. //Think of that as ObjC's base classes. You could have ObjC without
  16. //Foundation but you'll never see that in the wild.
  17. //All data types in ObjC are objects. Because objects are not C primitives
  18. //but, rather, complex structures we need a pointer to them rather than
  19. //an absolute reference. If you are going "Ah! Pointers! No!" don't worry,
  20. //you don't see them at all except when declaring a variable. When you
  21. //reference it you'd just use myString or whatever.
  22. //2. That (strong, nonatomic) stuff? That's ARC (automatic reference
  23. //counting) keywords. Those keywords help the ARC system manage memory
  24. //for you (gross oversimplification.) Don't worry about it.  You can
  25. //learn more later.
  26. //3. Notice the "@" before "property?" The "@" symbol tells the compiler
  27. //"Hey, what's coming next is some weird ObjectiveC shit, so don't interpret
  28. //it as C." ObjC breaks out the @ whenever it does something ObjC-ish. You'll
  29. //see it a lot. Notice it before interface and end too, to tell the compiler
  30. //"Hey, this is ObjC stuff."
  31.  
  32. - (void) doSomething;
  33. //This is a method declaration. You should understand this easily.
  34. //We're declaring a method called doSomething that returns nothing.
  35. //The - means it is an instance method. If it were a + it would be
  36. //a class method and you could call it on uninstantiated classes.
  37.  
  38. @end
  39.  
  40.  
  41. @implementation MyObject
  42.  
  43. @synthesize myString
  44. //What's this? Well, it's got a @ before it so we know it is weird
  45. //ObjC stuff. What's it do? Well, that's our property...
  46. //The synthesize directive auto-generates your getters and setters.
  47. //So instead of implementing some stupid setMyString or getMyString
  48. //the @synthesize directive does it for you for any property you've declared
  49. //Let me guess, you just said "Woah, that's awesome!" Shit yeah it is.
  50.  
  51. //And here's the meat of our method
  52. - (void) doSomething {
  53.    
  54.     [self setMyString: @"Yeah Booooiii"];
  55.     //This is the only part of ObjC that's really all that weird
  56.     //and, it's quite simple once you get used to it.
  57.     //The brackets enclosing this directive let us know we are
  58.     //sending a message to an object. You can read that as
  59.     //we're calling a method on an object. For now "send a message" == "call a method"
  60.     //in your brain. There are subtle differences, but they don't mean anything
  61.     //right now.
  62.     //So self is obvious. We're referring to ourself, like this in JS.
  63.     //What are we doing? We're calling setMyString: or, the setter that
  64.     //was generated by the @synthesize directive. After that we see the value
  65.     //we are sending. The @ before the value tells us that this is an NSString!
  66.     //Simple!
  67.     //in Java or JS or whatever that would simply look like:
  68.     //self.setMyString("Yeah Boooiii");
  69.  
  70. }
  71.  
  72. @end
Advertisement
Add Comment
Please, Sign In to add comment