oc基础知识总结

#import “” 表示引用本地文件// 自己创建
#import <> 表示引用库文件 // 系统文件
— 是实例调用的方法,类必须初始化后,用实例去调用
+ 方法是类方法,不需要实例化,直接使用类名调用。


NSNumber *number1 = [NSNumber numberWithInt:1000];
NSString* p1 = [number1 stringValue];
NSLog(@"%@",p1);//1000

NSNumber* number2 = [NSNumber numberWithFloat:23.2f];
NSString* p2 = [number2 stringValue];

NSLog(@"%@", p2);//23.2

//oc生成json 
NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
resultDict[@KChannelID] = [NSString stringWithFormat:@"%@",channelID];
resultDict[@KAgoraEcode] = [NSNumber numberWithInt:0];
NotifyEventListener(KOnChannelJoinedResult, ToJsonCString(resultDict));

//设置代理
    instance.onLoginSuccess = ^(uint32_t uid, int fd){
        onLoginSuccess(uid,fd);
    };

//打印
    NSLog(@"deviceID ====> %@",CStrToNSStr(deviceID));
    NSLog(@"retry_time_in_s ====> %d",retry_time_in_s);


//预处理代码
#define 命令之后为常量名,再之后为所赋的代替值,用空格隔开,结尾不需分号。
#define 常量名 代替值
//设置宏定义
    #define KOnChannelAttrUpdatedResult     "OnChannelAttrUpdatedResult"
    #define KAccount                "account"

//设置方法宏定义
#define CStrToNSStr(__cst__)  [U3DBridgeGlobal stringWithCString:__cst__]
#define NStrToCSStr(__nst__)  [U3DBridgeGlobal cstringWithString:__nst__]

/**
 *  String转换为CString
 *
 *  @param String
 *
 *  @return CString
 */
+ (const char *)cstringWithString:(NSString *)string {
    if (string) {
        return [string UTF8String];
    } else {
        return NULL;
    }
}
/**
 *  CString转换为String
 *
 *  @param CString
 *
 *  @return String
 */
+ (NSString *)stringWithCString:(const char *)cString {
    if (cString) {
        return [NSString stringWithUTF8String: cString];
    } else {
        return [NSString stringWithUTF8String: ""];
    }
}
/**
 *  U3D回调函数
 *
 *  @param u3DGameObjectOfCallback  回调GameObject
 *  @param u3DMethodOfCallback      回调函数
 *  @param param                    参数
 */
+ (void)sendMessageToU3D:(NSString *)objName u3DMethodOfCallback:(NSString *)methodName message:(NSString *)message {
    if (nil == message) {
        NSLog(@"xcode===>>>U3D %@.%@(nil) ", objName, methodName);
    } else {
        NSLog(@"xcode===>>>U3D %@.%@(%@) ", objName, methodName, message);
    }
    UnitySendMessage([self cstringWithString:objName], [self cstringWithString:methodName], [self cstringWithString:message]);
}
//遍历数组
   void onChannelUserList (NSMutableArray *accounts, NSMutableArray *uids){
       if(accounts != nil && accounts.count > 0){
           NSUInteger count = accounts.count;
           NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
           for (NSInteger i = 0; i < count; i++) {
               NSString *string = [accounts objectAtIndex:0];
               resultDict[[NSString stringWithFormat:@"User_%ld",(long)i]] = string;
               NSLog(@"account == > %@",string);
           }
           resultDict[@"count"] = [NSNumber numberWithInteger:count];
           NotifyEventListener(KOnChannelUserList,ToJsonCString(resultDict));
       }
   };


//创建对象 
CLASS *class= [[CLASS alloc] init];

//实现对象的单例
+ (CLASS *)shareInstance{
        static CLASS *instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
                instance = [[CLASS alloc] init];
        });
        return instance;
}



你可能感兴趣的:(Unity3D,工具合集)