//
// Info.h
// cts
//
// Created by jiangzhihao on 2018/11/9.
// Copyright © 2018年 蒋智昊. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Info : NSObject
{
}
// 括号外面可以申明属性和方法
// 用@property申明属性
// 说明:属性会自动合成setter和getter方法
@property int msgcode;
// 方法:方法分为对象方法和类方法
// 对象方法以-开头,只有这个类中的某个具体对象才能调用
// 类方法以+开头,类方法只能用类去调用
// 声明一个对象方法
- (NSString *)getAction:(NSString *)code;
// 声明一个类方法
+ (void)setAction;
@end
NS_ASSUME_NONNULL_END
//
// Info.m
// cts
//
// Created by jiangzhihao on 2018/11/9.
// Copyright © 2018年 蒋智昊. All rights reserved.
//
#import "Info.h"
@implementation Info
// 声明一个对象方法
- (NSString *)getAction:(NSString *)code
{
NSString *str = [NSString stringWithFormat:@"%@%@", @"hi", code];
return str;
}
// 声明一个类方法
+ (void)setAction
{
NSLog(@"action");
}
@end
// 调用类方法测试结果
[Info setAction];
// 创建对象info
Info *info =[[Info alloc]init];
NSString *say = [info getAction:@"0001"];
NSLog(@"%@", say);
// VC (Key Value Coding) Objective-C 允许以字符串形式间接操作对象的属性
// 这种方式的全称是 Key Value Coding (简称KVC),即键值编码
// 通过KVC方式设置属性
Info *inf = [[Info alloc] init];
[inf setValue:@1000 forKey:@"msgcode"];
// 通过KVC方式读取属性
NSString *msgcode = [inf valueForKey:@"msgcode"];
NSLog(@"%@", msgcode);