//
// main.m
// 常见错误
//
// Created by Caoxia on 15/11/10.
// Copyright © 2015年 Caoxia. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Demo : NSObject
{
int a;
}
- (void)method;
@end
@implementation Demo
- (void)method
{
NSLog(@"哈哈");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
/*
常见的错误有以下几种:
1.只有类的申明没有类的实现
@interface Demo : NSObject
{
int a;
}
- (void)Method;
@end
然后就没有然后了
2.漏了@end
@interface Demo : NSObject
{
int a;
}
- (void)Method;
然后就没有然后了 //正确做法是应该加上@end
@implementation Demo
- (void)Method
{
NSLog(@"牛逼");
}
然后也没有然后了//正确做法是应该加上@end
3.@interface和@implementation嵌套
@interface Demo : NSObject
{
int a;
}
- (void)method;
//写法真逗,这里需要加上@end
@implementation Demo
- (void)method
{
NSLog(@"哈哈");
}
@end
4.成员变量没有写在括号里
@interface Demo : NSObject
{
int a;
}
int b;//你是什么鬼,为什么不呆在括号里 //正确是是应该放在括号里
- (void)method;
@end
5.方法的申明写在了大括号里
@interface Demo : NSObject
{
int a;
- (void)method;//喂喂喂,哥们,呆在括号外面,这不是你呆的地方
}
@end
6.成员变量直接在括号中初始化,
@interface Demo : NSObject
{
int a = 5;//这里不能进行初始化操作,就是不能写=5
}
- (void)method;
@end
7.方法能当函数一样调用
@interface Demo : NSObject
{
int a ;
}
- (void)method;
@end
在调用的时候,不能直接写method();调用
8.C函数不能访问OC对象的成员
主函数
Demo *d = [Demo new];
void Cfunction()
{
d->_a = 5;//函数中访问对象的成员是错的
}
9.方法有申明,但是实现的时候写成了函数
@interface Demo : NSObject
{
int a ;
}
- (void)method;
@end
实现
void method()//哥们,你这实现的是函数,不是方法
{
......
}
注意点:
1.OC可以没有@interface,在没有@interface的情况下,同样可以定义一个类
*/
}
return 0;
}