objective-c Object对象 (4)

参考 http://xys289187120.blog.51cto.com/3361352/676668

关于如何用xcode新建一个文件,这里就不在写了。

创建一个Myclass.m和一个Myclass.h文件

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Myclass : NSObject
 4 {
 5     //成员变量
 6     NSString *mName;
 7     int mNumber;
 8 }
 9 
10 -(id) initMy;
11 -(void) setInfo:(NSString *) name :(int) number;
12 -(void) printInfo;
13 
14 @end

感觉和java挺像的。

成员变量和方法:方法包括返回值,方法名和参数。

 1 #import "Myclass.h"
 2 
 3 @implementation Myclass
 4 - (id) initMy
 5 {
 6     self = [super init];
 7     if (self) {
 8         // 在这里进行初始化的工作
 9         [self setInfo: @"雨松MOMO":9527];
10     }
11     
12     return self;
13 }
14 
15 -(void)setInfo:(NSString *) name: (int) number
16 {
17     //参数的传递赋值
18     mName = name;
19     mNumber = number;
20 }
21 
22 -(void)printInfo{
23     
24     //输出log信息
25     NSLog(@"我的名字: %@",mName);
26     NSLog(@"我的编号: %d",mNumber);
27 }
28 
29 @end

在要使用的文件里,import 对应的文件。声明一个指向Myclass的指针。

 1 #import <UIKit/UIKit.h>
 2 #import "Myclass.h"
 3 
 4 @interface HWViewController : UIViewController
 5 {
 6     //声明一个指向Myclass的指针
 7     Myclass *myClass;
 8 
 9 }
10 @end
 1 #import "HWViewController.h"
 2 
 3 @interface HWViewController ()
 4 
 5 @end
 6 
 7 @implementation HWViewController
 8 
 9 - (void)viewDidLoad
10 {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view, typically from a nib.
13 
14     
15     //创建对象
16     myClass = [[Myclass alloc] initMy];
17     //调用对象方法
18     [myClass printInfo];
19     //释放指针
20    // [myClass release];
21 }
22 
23 - (void)didReceiveMemoryWarning
24 {
25     [super didReceiveMemoryWarning];
26     // Dispose of any resources that can be recreated.
27 }
28 
29 @end

关于注释掉的release方法在后面的文章说明。

输出:

2013-12-25 11:39:09.550 Helloworld20131021_NSMutableString[1256:c07] 我的名字: 雨松MOMO

2013-12-25 11:39:09.551 Helloworld20131021_NSMutableString[1256:c07] 我的编号: 9527

学习过Java 或者 C 语言的朋友应该很清楚static关键字吧?在某个类中声明一个static 静态变量,其他类中想使用它或者修改它不用new 这个对象 ,直接使用它的类名方可直接拿到这个静态变量的对象,遍可以在其他类中任意修改这个变量的数值。

        

        Objective-C 的语法中声明后的static静态变量在其他类中是不能通过类名直接访问的,它的作用域只能是在声明的这个.m文件中 。不过可以调用这个类的方法间接的修改这个静态变量的值。MOMO通过一个例子详细的解析一下static关键字的使用。

1 #import <Foundation/Foundation.h>
2 
3 @interface MyClass : NSObject
4 +(void) addCount;
5 
6 @end

static的变量不能放在成员变量的里面。

 static关键字声明的变量必须放在implementation外面,或者方法中,如果不为它赋值默认为0,它只在程序开机初始化一次。

 1 #import "MyClass.h"
 2 
 3 static int sCount = 100;
 4 @implementation MyClass
 5 +(void) addCount{
 6     //static int sCount = 100;
 7     sCount++;
 8     NSLog(@"value of static :%d",sCount);
 9 }
10 @end

调用:

#import "MyClass.h"

static int sCount = 100;
@implementation MyClass
+(void) addCount{
    //static int sCount = 100;
    sCount++;
    NSLog(@"value of static :%d",sCount);
}
@end

输出:

2013-12-25 15:19:24.290 Helloworld20131021_Object[2076:c07] value of static :101

2013-12-25 15:19:24.327 Helloworld20131021_Object[2076:c07] value of static :102

2013-12-25 15:19:24.328 Helloworld20131021_Object[2076:c07] value of static :103

2013-12-25 15:19:24.328 Helloworld20131021_Object[2076:c07] value of static :104

2013-12-25 15:19:24.329 Helloworld20131021_Object[2076:c07] value of static :105

修改:

 1 #import "MyClass.h"
 2 
 3 //static int sCount = 100;
 4 @implementation MyClass
 5 +(void) addCount{
 6     static int sCount = 100;
 7     sCount++;
 8     NSLog(@"value of static :%d",sCount);
 9 }
10 @end

输出:

2013-12-25 15:20:27.436 Helloworld20131021_Object[2113:c07] value of static :101

2013-12-25 15:20:27.450 Helloworld20131021_Object[2113:c07] value of static :102

2013-12-25 15:20:27.451 Helloworld20131021_Object[2113:c07] value of static :103

2013-12-25 15:20:27.451 Helloworld20131021_Object[2113:c07] value of static :104

2013-12-25 15:20:27.452 Helloworld20131021_Object[2113:c07] value of static :105

可见即使将static静态变量写在方法中,它的初始化也是在程序开机时,程序一旦启动以后static是不能在创建的。所以程序在这里调用了5次这个方法,sCount的值并没有因为重新创建static sCount而改变,而是将sCount的值一直存在内存中。

 开始参考文章的时候出现过问题:

error:Cannot assign to 'self' outside of a method in the init family
原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init +大写字母开头+其他 为准则。例如:- (id) initWithXXX;
出错代码:- (id) Myinit{
self = [super init];
……
}
解决方法:- (id) initWithMy
{
self = [super init];
}

原文地址:https://www.cnblogs.com/aosting/p/3489522.html