Objective-C/C++
-
Objective-C Runtime https://www.iteye.com/blog/lobin-2524472
Objective-C由Brad Cox和Tom Love于1980年发明。
http://www.gnustep.org/resources/documentation/ObjectivCBook.pdf
Objective-C 1.0
Objective-C 1.0也称“traditional” Objective-C
Objective-C 2.0
从C到Objective-C
块
定义一个块变量
void (^LOG0) (NSString *);
void (^LOG) (NSString *, ...);
定义一个块变量并赋值
void (^LOG0) (NSString *) = ^(NSString *format) {
//NSLog(format);
NSLog(format, nil);
NSLog(@"%@", format);
//*
NSString *str = [NSString stringWithFormat:@"%@", format];
NSLog(@"%@",str);
//*/
};
void (^LOG) (NSString *, ...) = ^(NSString *format, ...) {
va_list va;
va_start(va, format);
NSLogv(format, va);
va_end(va);
};
等号后面是一个表示块的字面量(block literal)。
定义一个块类型
typedef void (^log0) (NSString *);
typedef void (^logx) (NSString *, ...);
log0 fn_log0 = LOG0;
logx fn_logx = LOG;
调用
LOG0(@"Hello, world.");
LOG(@"%@: Hello, world.", @"Cocoa");
fn_log0(@"Hello, world.");
fn_logx(@"%@: Hello, world.", @"Cocoa");
对象
调用
通常其他语言在调用一个对象方法时,通常是这样的:
obj.firstMethod();
obj.secondMethod(2);
而在Objective-C/C++语言中则是这样:
[obj firstMethod];
[obj secondMethod: 2];
调用机制
Objective-C/C++和其他面向对象语言不一样的是,调用对象方法在Objective-C/C++语言中是向对象发送一个消息,并接收返回一个消息。不管哪种方式,都本质上都是对象间通信。
这实际上是通过objc_msgSend发送一个消息给对象obj。objc_msgSend是Objective-C/C++运行时库提供的函数。我们也可以通过objc_msgSend向对象发送和接收消息。
发送和接收消息
OBJC_EXPORT void objc_msgSend(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
OBJC_EXPORT id objc_msgSend(id self, SEL op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
typedef struct objc_selector *SEL;
objc_msgSendSuper
OBJC_EXPORT void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
OBJC_EXPORT id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
除了以上方式,还可以通过performSelector、类似反射方式以及NSInvocation来向对象发送和接收消息。performSelector方式使用起来不是很方便,只能接受不超过2个的参数个数。
通过performSelector来向对象发送和接收消息
- (void) firstMethod;
- (void) secondMethod: (int) arg;
SimpleClass *obj = [SimpleClass new];
[obj performSelector:@selector(firstMethod)]; // just only without argument.
[obj performSelector:@selector(secondMethod:) withObject: (id) 2]; // just only with one argument.
通过类似反射方式来向对象发送和接收消息
- (void) firstMethod;
SimpleClass *obj = [SimpleClass new];
void (*firstMethod)(id, SEL); // - (void) firstMethod;
firstMethod = (void (*) (id, SEL)) [obj methodForSelector: @selector(firstMethod)];
firstMethod(obj, @selector(firstMethod));
通过NSInvocation来向对象发送和接收消息
- (void) secondMethod: (int) arg;
SimpleClass *obj = [SimpleClass new];
NSMethodSignature *secondMethodSignature = [obj methodSignatureForSelector: @selector(secondMethod:)];
NSInvocation *secondMethodInvocation = [NSInvocation invocationWithMethodSignature: secondMethodSignature];
[secondMethodInvocation setTarget:obj];
[secondMethodInvocation setSelector:@selector(secondMethod:)];
int arg = 2000;
[secondMethodInvocation setArgument:&arg atIndex:2];
[secondMethodInvocation invoke];
类
■ An interface that declares the methods and instance variables of the class and names its superclass
■ An implementation that actually defines the class (contains the code that implements its methods)
NSObject
所有类的基类。NSObject不是所有其他类的隐式基类,需要显式的从NSObject继承。NSObject在Foundation中定义。
new方法覆盖(Override)
+ (id)new { id handle = [super new]; printf("new of BaseClass.\n"); return handle; }
alloc方法覆盖(Override)
+ (id)alloc { id handle = [super alloc]; printf("alloc of BaseClass.\n"); return handle; }
init方法覆盖(Override)
- (id)init { self = [super init]; printf("init of BaseClass.\n"); return self; }
Objective-C混合编程
Runtime
Objective-C/C++编程中可以直接调用Runtime运行时库提供的底层功能。比如直接调用objc_msgSend向对象发送消息。
objc_msgSend
- (void) firstMethod;
- (void) secondMethod: (int) arg;
SimpleClass *obj = [SimpleClass new];
SEL firstMethodSel = @selector(firstMethod);
objc_msgSend((id)obj, firstMethodSel); // [obj firstMethod];
SEL secondMethodSel = @selector(secondMethod:);
objc_msgSend((id)obj, secondMethodSel, 2); // [obj secondMethod: 2];
- (int) firstMethodWithRet;
- (int) secondMethodWithRet: (int) arg;
SEL firstMethodWithRetSel = @selector(firstMethodWithRet);
int ret = (int) objc_msgSend((id)obj, firstMethodWithRetSel); // int ret = [obj firstMethodWithRet];
printf("firstMethodWithRet. ret=%d\n", ret);
SEL secondMethodWithRetSel = @selector(secondMethodWithRet:);
ret = (int) objc_msgSend((id)obj, secondMethodWithRetSel, 2); // int ret = [obj secondMethodWithRet: 2];
printf("secondMethodWithRet. ret=%d\n", ret);
调用C静态链接库
#include <stddef.h> #if ! defined(QT) #define QT struct qt { size_t size; char data[]; }; struct qt* qt(int i); int qt_get(struct qt* v); int qt_destroy(struct qt* v); #endif
#include<stdlib.h> #include<string.h> #include"qt.h" struct qt* qt(int i) { size_t size = sizeof(i); struct qt* v = (struct qt*) malloc(sizeof(struct qt) + size); if (v != NULL) { v->size = size; memcpy(v->data, &i, sizeof(i)); } return v; } int qt_get(struct qt* v) { int *p = NULL; if (v == NULL) { return 0; } p = (int *) v->data; return *p; } int qt_destroy(struct qt* v) { free(v); return 0; }
编译
# gcc -c qt.c -o qt.o
生成静态库
# ar rc libqt.a qt.o
#include <stdio.h> #include "qt.h" #import <Foundation/Foundation.h> int main(int argc, char *argv[]) { int i = 1413; struct qt* v = qt(i); if (v == NULL) { return 1; } printf("qt: i=%d\n", qt_get(v)); NSLog(@"qt: i=%d", qt_get(v)); qt_destroy(v); return 0; }
gcc -framework Foundation -L . libqt_test.m -o libqt_test -lqt
编写静态链接库
#include <stddef.h> #if ! defined(QT) #define QT struct qt { size_t size; char data[]; }; struct qt* qt(int i); int qt_get(struct qt* v); int qt_destroy(struct qt* v); #endif
其实就是把上面的.c文件改为.m文件即可。
#include<stdlib.h> #include<string.h> #include"qt.h" struct qt* qt(int i) { size_t size = sizeof(i); struct qt* v = (struct qt*) malloc(sizeof(struct qt) + size); if (v != NULL) { v->size = size; memcpy(v->data, &i, sizeof(i)); } return v; } int qt_get(struct qt* v) { int *p = NULL; if (v == NULL) { return 0; } p = (int *) v->data; return *p; } int qt_destroy(struct qt* v) { free(v); return 0; }
编译
# gcc -c qt.m -o qt.o
生成静态库
# ar rc libqt.a qt.o
#include <stdio.h> #include "qt.h" #import <Foundation/Foundation.h> int main(int argc, char *argv[]) { int i = 1413; struct qt* v = qt(i); if (v == NULL) { return 1; } printf("qt: i=%d\n", qt_get(v)); NSLog(@"qt: i=%d", qt_get(v)); qt_destroy(v); return 0; }
gcc -framework Foundation -L . libqt_test.m -o libqt_test -lqt
编译
$ gcc -framework Foundation hello.m -o hello -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.m -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 305 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0 -fdebug-compilation-dir /root/objective-c -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fobjc-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/2l/yzjcl8j56693p3y40qcnr4zc0000gn/T/hello-313d89.o -x objective-c hello.m
clang -cc1 version 9.0.0 (clang-900.0.39.2) default target x86_64-apple-darwin16.7.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o hello -framework Foundation /var/folders/2l/yzjcl8j56693p3y40qcnr4zc0000gn/T/hello-313d89.o -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
相关推荐
Objective-C与Objective-C++的混用代码示例 可以学习学习
所以有一定C/C++语言基础理解和掌握Objective-C也会相应的快些。这回,我们将比较着学习 Objective-C语言,掌握其语法并理解其思想。 语法 让我们先来看看C++和Objective-C中对于类的宣言 : C++ #...
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
它是 C 语言的超集,结合了 Smalltalk 式的消息传递机制和 C++ 的特性。Objective-C 是苹果公司采用的主要编程语言之一,也是开发 macOS 和 iOS 应用程序的主要语言之一。Objective-C 是一种强大而灵活的编程语言,...
C++ Objective-C 快速 精通,使C++程序员快速掌握Objective-C
NULL 博文链接:https://eric-gao.iteye.com/blog/1637312
贝塔风谷修复项目_Objective-C_C++_下载.zip
用于括号的 C++/Objective-C 开发包 使用 C、C++、Objective-C 或 Objective-C++ 时括号的增强功能: Objective-C/C++ 语法高亮。 将 .xib 文件高亮显示为 XML Ctrl-T(快速查找定义)用于在文件中导航 #include行...
c c++ objective-c 混合编程 头文件问题
该汇总包括3本书,非常有用。 1、Objective-C基础教程.pdf 2、Objective-C.2.0程序设计.pdf 3、从c++到Objective-C.pdf
本文主要介绍C/C++在Java、Android和Objective-C三大平台下实现混合编程,这里举例说明实现不同平台用C/C++实现编程的方法,有兴趣的小伙伴可以参考下
一个Objective-C简单继承代码,“学生”类,“老师”类继承“人”类
From C++ to Objective-C(中文版),iphone开发者必备。
MYO-ObjectiveC-OSX 用于 c++ Myo OS X sdk 的 Objective-C 包装器您需要将 myo.framework 添加到项目 -开始初始化并连接 Myo Myo *newMyo = [[Myo alloc] initWithApplicationIdentifier:@"com.example.myoobjc"];...
QQ和TouchID等手势锁屏,高仿QQ手势锁屏,手指解锁_Objective-C_C++_下载.zip
帮助C++开发者,快速学习Objective-C语言核心语法。 一个非常实用的帮助文档
Using C++ With Objective-C 苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++。有了它,你可以在Objective-C应用程序中使用已有的C++类库。 ...
如果你想迅速掌握Objective-C,并有c++或Java的基础,该文档绝对是你的首选。该文档共13页,全是Objective-C知识的精华,没有在此啰嗦与其他语言相同的部分。
《effective objective-c 2.0:编写高质量ios与os x代码的52个有效方法》是世界级c++开发大师scott meyers亲自担当顾问编辑的“effective software development series”系列丛书中的新作,amazon全五星评价。...