mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[UPDATE] Object Creation
This commit is contained in:
parent
50c3526247
commit
0d41a64056
@ -201,27 +201,56 @@ int main (int argc, const char * argv[])
|
|||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
// Declare your class in a header(.h) file:
|
// Declare your class in a header(.h) file:
|
||||||
|
// Class Declaration Syntax:
|
||||||
@interface UserObject : NSObject
|
// @interface <class name> : <parent classname>
|
||||||
|
// {
|
||||||
|
// Member variable declarations;
|
||||||
|
// }
|
||||||
|
// -/+ (type) Method declarations;
|
||||||
|
// @end
|
||||||
|
@interface MyClass : NSObject
|
||||||
{
|
{
|
||||||
// instance variables
|
int count;
|
||||||
|
id data;
|
||||||
|
NSString *name;
|
||||||
}
|
}
|
||||||
|
// Create the public getter/setter for the variable count
|
||||||
|
@property(assign) int count;
|
||||||
|
|
||||||
// Class method
|
// Methods
|
||||||
|
+/- (return type)methodSignature:(Parameter Type *)parameterName;
|
||||||
|
|
||||||
|
// + for class method
|
||||||
+ (NSString *)classMethod;
|
+ (NSString *)classMethod;
|
||||||
|
|
||||||
// Instance method
|
// - for instance method
|
||||||
- (NSString *)instanceMethodWithParmeter:(NSString *)string;
|
- (NSString *)instanceMethodWithParmeter:(NSString *)string;
|
||||||
|
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;
|
||||||
|
-
|
||||||
@end
|
@end
|
||||||
|
|
||||||
// Implement the methods in an implementation (.m) file:
|
// Implement the methods in an implementation (.m) file:
|
||||||
|
|
||||||
@implementation UserObject
|
@implementation UserObject
|
||||||
|
|
||||||
|
// Constructors are a way of creating classes
|
||||||
|
// This is a default constructor
|
||||||
|
- (id)init
|
||||||
|
{
|
||||||
|
if ((self = [super init]))
|
||||||
|
{
|
||||||
|
self.count = 1;
|
||||||
|
|
||||||
|
// Create an object instance by allocating memory and initializing it.
|
||||||
|
// An object is not fully functional until both steps have been completed.
|
||||||
|
UserObject *someObject = [[UserObject alloc] init];
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
+ (NSString *)classMethod
|
+ (NSString *)classMethod
|
||||||
{
|
{
|
||||||
return @"SomeString";
|
return [[self alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)instanceMethodWithParmeter:(NSString *)string
|
- (NSString *)instanceMethodWithParmeter:(NSString *)string
|
||||||
@ -236,9 +265,6 @@ int main (int argc, const char * argv[])
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed.
|
|
||||||
UserObject *someObject = [[UserObject alloc] init];
|
|
||||||
|
|
||||||
##Calling Methods
|
##Calling Methods
|
||||||
|
|
||||||
// The Objective-C model of object-oriented programming is based on message passing to object instances.
|
// The Objective-C model of object-oriented programming is based on message passing to object instances.
|
||||||
|
Loading…
Reference in New Issue
Block a user