一、创建步骤
1.打开xcode
2.创建工程,点击commend line
3.创建Cocoa文件
4.弄清文件作用
.h文件:写接口,在此文件进行写属性和类方法(+),实例函数(-);
注:省去setter和getter可以使用 @property
() 有strong ,readwrite等类型
文件示例:
//
// Car.h
// ClassDemo
//
// Created by LYF on 2020/9/5.
// Copyright © 2020 LYF. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Car : NSObject{
@private
NSString *name_;
@private
int age;
}
// 定义属性
@property(strong) NSString *name;
@property(readwrite) int age;// 不能使用strong,strong用于对象
// 方法
// 类方法 ,对象需要指针;例如(NSString *)
+(void)sendThisDate:(NSDate *) date;
+(void)testNum:(int) number;
-(void)write:(NSString *)things;
@end
NS_ASSUME_NONNULL_END
.m文件:实现,实现接口的文件
示例:
//
// Car.m
// ClassDemo
//
// Created by LYF on 2020/9/5.
// Copyright © 2020 LYF. All rights reserved.
//
#import "Car.h"
@implementation Car
@synthesize age; // 省去setter和getter
-(void) setName:(NSString *)name{
name_=name;
}
-(NSString *)name{
return name_;
}
// 类方法实现
+(void)sendThisDate:(NSDate *)date{
NSLog(@"今天是%@",date);
}
+(void)testNum:(int)number{
NSLog(@"test number is %d",number);
}
-(void)write:(NSString *)things{
NSLog(@"things are %@",things);// %@:字符串 %s 字符char %d int
}
@end
二、关键代码
AppDelegate.h
//
// AppDelegate.h
// ClassDemo
//
// Created by LYF on 2020/9/5.
// Copyright © 2020 LYF. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) NSWindow *window;
@end
NS_ASSUME_NONNULL_END
AppDelegate.m
//
// AppDelegate.m
// ClassDemo
//
// Created by LYF on 2020/9/5.
// Copyright © 2020 LYF. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window=_window;
-(void)applicaitonDidFinishLauching:(NSNotification *)aNotification{
NSLog(@"Mac app finished lauching");
}
-(void)changeColor{
self.window.backgroundColor = [NSColor blueColor];
}
-(void)applicationDidFinishLaunching:(NSNotification *)anotification{
NSLog(@"结束加载");
NSButton *button=[NSButton new];
button = [[NSButton alloc] initWithFrame:NSMakeRect(230, 200, 100, 40)];
[button setTitle:@"change color"];
[button setButtonType:NSMomentaryLightButton];
[button setTarget:self];
[button setAction:@selector(changeColor)];
[[self.window contentView] addSubview: button];
NSTextField *text=[NSTextField new];
text=[[NSTextField alloc] initWithFrame:NSMakeRect(100, 100, 300, 30)];
[[self.window contentView] addSubview:text];
}
@end
main.m
//
// main.m
// ClassDemo
//
// Created by LYF on 2020/9/5.
// Copyright © 2020 LYF. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Car2.h"
#import "ClassLearn/Car.h" // 表明引入路径 默认从ClassDemo开始
#import "ClassLearn/practice.h"
#import "AppDelegate.h"
@class Car;
@class Car2;
@class practice;
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSApplication *macApp = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
macApp.delegate = appDelegate;
int style = NSClosableWindowMask | NSResizableWindowMask | NSTexturedSquareBezelStyle | NSTitledWindowMask | NSMiniaturizableWindowMask;
NSWindow *appWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(50, 50, 600, 400) styleMask:style backing:NSBackingStoreBuffered defer:NO];
appWindow.backgroundColor=[NSColor redColor];
appDelegate.window = appWindow;
[appWindow makeKeyAndOrderFront:appWindow];
[macApp run];
}
return 0;
}