[TOC]
runtime相關問題
- 面試題出自掘金的一篇文章《阿里、字節:一套高效的iOS面試題》
- 該面試題解答github 地址版本目前已經完結,可自行下載pdf進行閱讀,僅做參考,對于有問題的解答可提 issue,歡迎 star fork。
- 調試好可運行的源碼 objc-runtime,官網找 objc4;
- 歡迎轉載,轉載請注明出處:pmst-swiftgg
結構模型
1. 介紹下runtime的內存模型(isa、對象、類、metaclass、結構體的存儲信息等)
2. 為什么要設計metaclass
3. class_copyIvarList
& class_copyPropertyList
區別
class_copyIvarList
獲取類對象中的所有實例變量信息,從 class_ro_t
中獲取:
Ivar *
class_copyIvarList(Class cls, unsigned int *outCount)
{
const ivar_list_t *ivars;
Ivar *result = nil;
unsigned int count = 0;
if (!cls) {
if (outCount) *outCount = 0;
return nil;
}
mutex_locker_t lock(runtimeLock);
assert(cls->isRealized());
if ((ivars = cls->data()->ro->ivars) && ivars->count) {
result = (Ivar *)malloc((ivars->count+1) * sizeof(Ivar));
for (auto& ivar : *ivars) {
if (!ivar.offset) continue; // anonymous bitfield
result[count++] = &ivar;
}
result[count] = nil;
}
if (outCount) *outCount = count;
return result;
}
class_copyPropertyList
獲取類對象中的屬性信息, class_rw_t
的 properties
,先后輸出了 category / extension/ baseClass 的屬性,而且僅輸出當前的類的屬性信息,而不會向上去找 superClass 中定義的屬性。
objc_property_t *
class_copyPropertyList(Class cls, unsigned int *outCount)
{
if (!cls) {
if (outCount) *outCount = 0;
return nil;
}
mutex_locker_t lock(runtimeLock);
checkIsKnownClass(cls);
assert(cls->isRealized());
auto rw = cls->data();
property_t **result = nil;
unsigned int count = rw->properties.count();
if (count > 0) {
result = (property_t **)malloc((count + 1) * sizeof(property_t *));
count = 0;
for (auto& prop : rw->properties) {
result[count++] = ∝
}
result[count] = nil;
}
if (outCount) *outCount = count;
return (objc_property_t *)result;
}
Q1:
class_ro_t
中的baseProperties
呢?Q2:
class_rw_t
中的properties
包含了所有屬性,那何時注入進去的呢? 答案見 5.
4. class_rw_t
和 class_ro_t
的區別
測試發現,class_rw_t
中的 properties
屬性按順序包含分類/擴展/基類中的屬性。
struct class_ro_t {
uint32_t flags;
uint32_t instanceStart;
uint32_t instanceSize;
#ifdef __LP64__
uint32_t reserved;
#endif
const uint8_t * ivarLayout;
const char * name;
method_list_t * baseMethodList;
protocol_list_t * baseProtocols;
const ivar_list_t * ivars;
const uint8_t * weakIvarLayout;
property_list_t *baseProperties;
method_list_t *baseMethods() const {
return baseMethodList;
}
};
struct class_rw_t {
// Be warned that Symbolication knows the layout of this structure.
uint32_t flags;
uint32_t version;
const class_ro_t *ro;
method_array_t methods;
property_array_t properties;
protocol_array_t protocols;
Class firstSubclass;
Class nextSiblingClass;
char *demangledName;
#if SUPPORT_INDEXED_ISA
uint32_t index;
#endif
}
5. category
如何被加載的,兩個category的load
方法的加載順序,兩個category的同名方法的加載順序
... -> realizeClass -> methodizeClass(用于Attach categories)-> attachCategories
關鍵就是在 methodizeClass 方法實現中
static void methodizeClass(Class cls)
{
runtimeLock.assertLocked();
bool isMeta = cls->isMetaClass();
auto rw = cls->data();
auto ro = rw->ro;
// =======================================
// 省略.....
// =======================================
property_list_t *proplist = ro->baseProperties;
if (proplist) {
rw->properties.attachLists(&proplist, 1);
}
// =======================================
// 省略.....
// =======================================
// Attach categories.
category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
attachCategories(cls, cats, false /*don't flush caches*/);
// =======================================
// 省略.....
// =======================================
if (cats) free(cats);
}
上面代碼能確定 baseProperties 在前,category 在后,但決定順序的是 rw->properties.attachLists
這個方法:
property_list_t *proplist = ro->baseProperties;
if (proplist) {
rw->properties.attachLists(&proplist, 1);
}
/// category 被附加進去
void attachLists(List* const * addedLists, uint32_t addedCount) {
if (addedCount == 0) return;
if (hasArray()) {
// many lists -> many lists
uint32_t oldCount = array()->count;
uint32_t newCount = oldCount + addedCount;
setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
array()->count = newCount;
// 將舊內容移動偏移量 addedCount 然后將 addedLists copy 到起始位置
/*
struct array_t {
uint32_t count;
List* lists[0];
};
*/
memmove(array()->lists + addedCount, array()->lists,
oldCount * sizeof(array()->lists[0]));
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
}
else if (!list && addedCount == 1) {
// 0 lists -> 1 list
list = addedLists[0];
}
else {
// 1 list -> many lists
List* oldList = list;
uint32_t oldCount = oldList ? 1 : 0;
uint32_t newCount = oldCount + addedCount;
setArray((array_t *)malloc(array_t::byteSize(newCount)));
array()->count = newCount;
if (oldList) array()->lists[addedCount] = oldList;
memcpy(array()->lists, addedLists,
addedCount * sizeof(array()->lists[0]));
}
}
所以 category 的屬性總是在前面的,baseClass的屬性被往后偏移了。
Q1:那么多個 category 的順序呢?答案見6
2020/03/18 補充下應用程序 image 鏡像加載到內存中時, Category 解析的過程,注意下面的 while(i--)
這里倒敘將 category 中的協議 方法 屬性添加到了 rw = cls->data()
中的 methods/properties/protocols
中。
static void
attachCategories(Class cls, category_list *cats, bool flush_caches)
{
if (!cats) return;
if (PrintReplacedMethods) printReplacements(cls, cats);
bool isMeta = cls->isMetaClass();
// fixme rearrange to remove these intermediate allocations
method_list_t **mlists = (method_list_t **)
malloc(cats->count * sizeof(*mlists));
property_list_t **proplists = (property_list_t **)
malloc(cats->count * sizeof(*proplists));
protocol_list_t **protolists = (protocol_list_t **)
malloc(cats->count * sizeof(*protolists));
// Count backwards through cats to get newest categories first
int mcount = 0;
int propcount = 0;
int protocount = 0;
int i = cats->count;
bool fromBundle = NO;
while (i--) {
auto& entry = cats->list[i];
method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
if (mlist) {
mlists[mcount++] = mlist;
fromBundle |= entry.hi->isBundle();
}
property_list_t *proplist =
entry.cat->propertiesForMeta(isMeta, entry.hi);
if (proplist) {
proplists[propcount++] = proplist;
}
protocol_list_t *protolist = entry.cat->protocols;
if (protolist) {
protolists[protocount++] = protolist;
}
}
auto rw = cls->data();
// 注意下面的代碼,上面采用倒敘遍歷方式,所以后編譯的 category 會先add到數組的前部
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
rw->methods.attachLists(mlists, mcount);
free(mlists);
if (flush_caches && mcount > 0) flushCaches(cls);
rw->properties.attachLists(proplists, propcount);
free(proplists);
rw->protocols.attachLists(protolists, protocount);
free(protolists);
}
6. category
& extension
區別,能給NSObject添加Extension嗎,結果如何
category:
- 運行時添加分類屬性/協議/方法
- 分類添加的方法會“覆蓋”原類方法,因為方法查找的話是從頭至尾,一旦查找到了就停止了
- 同名分類方法誰生效取決于編譯順序,image 讀取的信息是倒敘的,所以編譯越靠后的越先讀入
- 名字相同的分類會引起編譯報錯;
extension:
- 編譯時決議
- 只以聲明的形式存在,多數情況下就存在于 .m 文件中;
- 不能為系統類添加擴展
7. 消息轉發機制,消息轉發機制和其他語言的消息機制優劣對比
8. 在方法調用的時候,方法查詢-> 動態解析-> 消息轉發
之前做了什么
9. IMP
、SEL
、Method
的區別和使用場景
三者的定義:
typedef struct method_t *Method;
using MethodListIMP = IMP;
struct method_t {
SEL name;
const char *types;
MethodListIMP imp;
};
Method 同樣是個對象,封裝了方法名和實現,關于 Type Encodings。
Code | Meaning |
---|---|
c |
A char
|
i |
An int
|
s |
A short
|
l |
A long``l is treated as a 32-bit quantity on 64-bit programs. |
q |
A long long
|
C |
An unsigned char
|
I |
An unsigned int
|
S |
An unsigned short
|
L |
An unsigned long
|
Q |
An unsigned long long
|
f |
A float
|
d |
A double
|
B |
A C++ bool or a C99 _Bool
|
v |
A void
|
* |
A character string (char * ) |
@ |
An object (whether statically typed or typed id ) |
# |
A class object (Class ) |
: |
A method selector (SEL ) |
[array type] | An array |
{name=type...} | A structure |
(name=type...) | A union |
b num |
A bit field of num bits |
^ type |
A pointer to type |
? |
An unknown type (among other things, this code is used for function pointers) |
-(void)hello:(NSString *)name
encode 下就是 v@:@
。
10. load
、initialize
方法的區別什么?在繼承關系中他們有什么區別
load 方法調用時機,而且只調用當前類本身,不會調用superClass 的 +load
方法:
void
load_images(const char *path __unused, const struct mach_header *mh)
{
// Return without taking locks if there are no +load methods here.
if (!hasLoadMethods((const headerType *)mh)) return;
recursive_mutex_locker_t lock(loadMethodLock);
// Discover load methods
{
mutex_locker_t lock2(runtimeLock);
prepare_load_methods((const headerType *)mh);
}
// Call +load methods (without runtimeLock - re-entrant)
call_load_methods();
}
void call_load_methods(void)
{
static bool loading = NO;
bool more_categories;
loadMethodLock.assertLocked();
// Re-entrant calls do nothing; the outermost call will finish the job.
if (loading) return;
loading = YES;
void *pool = objc_autoreleasePoolPush();
do {
// 1. Repeatedly call class +loads until there aren't any more
while (loadable_classes_used > 0) {
call_class_loads();
}
// 2. Call category +loads ONCE
more_categories = call_category_loads();
// 3. Run more +loads if there are classes OR more untried categories
} while (loadable_classes_used > 0 || more_categories);
objc_autoreleasePoolPop(pool);
loading = NO;
}
+initialize
實現
void _class_initialize(Class cls)
{
assert(!cls->isMetaClass());
Class supercls;
bool reallyInitialize = NO;
// Make sure super is done initializing BEFORE beginning to initialize cls.
// See note about deadlock above.
supercls = cls->superclass;
if (supercls && !supercls->isInitialized()) {
_class_initialize(supercls);
}
// Try to atomically set CLS_INITIALIZING.
{
monitor_locker_t lock(classInitLock);
if (!cls->isInitialized() && !cls->isInitializing()) {
cls->setInitializing();
reallyInitialize = YES;
}
}
if (reallyInitialize) {
// We successfully set the CLS_INITIALIZING bit. Initialize the class.
// Record that we're initializing this class so we can message it.
_setThisThreadIsInitializingClass(cls);
if (MultithreadedForkChild) {
// LOL JK we don't really call +initialize methods after fork().
performForkChildInitialize(cls, supercls);
return;
}
// Send the +initialize message.
// Note that +initialize is sent to the superclass (again) if
// this class doesn't implement +initialize. 2157218
if (PrintInitializing) {
_objc_inform("INITIALIZE: thread %p: calling +[%s initialize]",
pthread_self(), cls->nameForLogging());
}
// Exceptions: A +initialize call that throws an exception
// is deemed to be a complete and successful +initialize.
//
// Only __OBJC2__ adds these handlers. !__OBJC2__ has a
// bootstrapping problem of this versus CF's call to
// objc_exception_set_functions().
#if __OBJC2__
@try
#endif
{
callInitialize(cls);
if (PrintInitializing) {
_objc_inform("INITIALIZE: thread %p: finished +[%s initialize]",
pthread_self(), cls->nameForLogging());
}
}
#if __OBJC2__
@catch (...) {
if (PrintInitializing) {
_objc_inform("INITIALIZE: thread %p: +[%s initialize] "
"threw an exception",
pthread_self(), cls->nameForLogging());
}
@throw;
}
@finally
#endif
{
// Done initializing.
lockAndFinishInitializing(cls, supercls);
}
return;
}
else if (cls->isInitializing()) {
// We couldn't set INITIALIZING because INITIALIZING was already set.
// If this thread set it earlier, continue normally.
// If some other thread set it, block until initialize is done.
// It's ok if INITIALIZING changes to INITIALIZED while we're here,
// because we safely check for INITIALIZED inside the lock
// before blocking.
if (_thisThreadIsInitializingClass(cls)) {
return;
} else if (!MultithreadedForkChild) {
waitForInitializeToComplete(cls);
return;
} else {
// We're on the child side of fork(), facing a class that
// was initializing by some other thread when fork() was called.
_setThisThreadIsInitializingClass(cls);
performForkChildInitialize(cls, supercls);
}
}
else if (cls->isInitialized()) {
// Set CLS_INITIALIZING failed because someone else already
// initialized the class. Continue normally.
// NOTE this check must come AFTER the ISINITIALIZING case.
// Otherwise: Another thread is initializing this class. ISINITIALIZED
// is false. Skip this clause. Then the other thread finishes
// initialization and sets INITIALIZING=no and INITIALIZED=yes.
// Skip the ISINITIALIZING clause. Die horribly.
return;
}
else {
// We shouldn't be here.
_objc_fatal("thread-safe class init in objc runtime is buggy!");
}
}
void callInitialize(Class cls)
{
((void(*)(Class, SEL))objc_msgSend)(cls, SEL_initialize);
asm("");
}
注意看上面的調用了 callInitialize(cls)
然后又調用了 lockAndFinishInitializing(cls, supercls)
。
摘自iOS App冷啟動治理 一文中對 Dyld 在各階段所做的事情:
階段 | 工作 |
---|---|
加載動態庫 | Dyld從主執行文件的header獲取到需要加載的所依賴動態庫列表,然后它需要找到每個 dylib,而應用所依賴的 dylib 文件可能會再依賴其他 dylib,所以所需要加載的是動態庫列表一個遞歸依賴的集合 |
Rebase和Bind | - Rebase在Image內部調整指針的指向。在過去,會把動態庫加載到指定地址,所有指針和數據對于代碼都是對的,而現在地址空間布局是隨機化,所以需要在原來的地址根據隨機的偏移量做一下修正 - Bind是把指針正確地指向Image外部的內容。這些指向外部的指針被符號(symbol)名稱綁定,dyld需要去符號表里查找,找到symbol對應的實現 |
Objc setup | - 注冊Objc類 (class registration) - 把category的定義插入方法列表 (category registration) - 保證每一個selector唯一 (selector uniquing) |
Initializers | - Objc的+load()函數 - C++的構造函數屬性函數 - 非基本類型的C++靜態全局變量的創建(通常是類或結構體) |
最后 dyld 會調用 main() 函數,main() 會調用 UIApplicationMain(),before main()的過程也就此完成。
11. 說說消息轉發機制的優劣
內存管理
1.weak
的實現原理?SideTable
的結構是什么樣的
解答參考自瓜神的 weak 弱引用的實現方式 。
NSObject *p = [[NSObject alloc] init];
__weak NSObject *p1 = p;
// ====> 底層是runtime的 objc_initWeak
// xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-13.2 main.m 得不到下面的代碼,還是說命令參數不對。
NSObject objc_initWeak(&p, 對象指針);
通過 runtime 源碼可以看到 objc_initWeak
實現:
id
objc_initWeakOrNil(id *location, id newObj)
{
if (!newObj) {
*location = nil;
return nil;
}
return storeWeak<DontHaveOld, DoHaveNew, DontCrashIfDeallocating>
(location, (objc_object*)newObj);
}
SideTable 結構體在 runtime 底層用于引用計數和弱引用關聯表,其數據結構是這樣:
struct SideTable {
// 自旋鎖
spinlock_t slock;
// 引用計數
RefcountMap refcnts;
// weak 引用
weak_table_t weak_table;
}
struct weak_table_t {
// 保存了所有指向指定對象的 weak 指針
weak_entry_t *weak_entries;
// 存儲空間
size_t num_entries;
// 參與判斷引用計數輔助量
uintptr_t mask;
// hash key 最大偏移值
uintptr_t max_hash_displacement;
};
根據對象的地址在緩存中取出對應的 SideTable
實例:
static SideTable *tableForPointer(const void *p)
或者如上面源碼中 &SideTables()[newObj]
方式取表,這里的 newObj 是實例對象用其指針作為 key 拿到 從全局的 SideTables 中拿到實例自身對應的那張 SideTable。
static StripedMap<SideTable>& SideTables() {
return *reinterpret_cast<StripedMap<SideTable>*>(SideTableBuf);
}
取出實例方法的實現中,使用了 C++ 標準轉換運算符 reinterpret_cast ,其表達方式為:
reinterpret_cast <new_type> (expression)
每一個 weak 關鍵字修飾的對象都是用 weak_entry_t
結構體來表示,所以在實例中聲明定義的 weak 對象都會被封裝成 weak_entry_t
加入到該 SideTable 中 weak_table
中
typedef objc_object ** weak_referrer_t;
struct weak_entry_t {
DisguisedPtr<objc_object> referent;
union {
struct {
weak_referrer_t *referrers;
uintptr_t out_of_line : 1;
uintptr_t num_refs : PTR_MINUS_1;
uintptr_t mask;
uintptr_t max_hash_displacement;
};
struct {
// out_of_line=0 is LSB of one of these (don't care which)
weak_referrer_t inline_referrers[WEAK_INLINE_COUNT];
};
}
舊對象解除注冊操作 weak_unregister_no_lock
和 新對象添加注冊操作 weak_register_no_lock
,具體實現可前往 runtime 源碼中查看或查看瓜的博文。
weak
關鍵字修飾的對象有兩種情況:棧上和堆上。上圖主要解釋 id referent_id 和 id *referrer_id
,
- 如果是棧上,
referrer
值為 0x77889900,referent
值為 0x11223344 - 如果是堆上 ,
referrer
值為 0x1100000+ offset(也就是 weak a 所在堆上的地址),referent
值為 0x11223344。
如此現在類 A 的實例對象有兩個 weak 變量指向它,一個在堆上,一個在棧上。
void
weak_unregister_no_lock(weak_table_t *weak_table, id referent_id,
id *referrer_id)
{
objc_object *referent = (objc_object *)referent_id; // 0x11223344
objc_object **referrer = (objc_object **)referrer_id; // 0x77889900
weak_entry_t *entry;
if (!referent) return;
// 從 weak_table 中找到 referent 也就是上面類A的實例對象
if ((entry = weak_entry_for_referent(weak_table, referent))) {
// 在 entry 結構體中的 referrers 數組中找到指針 referrer 所在位置
// 將原本存儲 referrer 值的位置置為 nil,相當于做了一個解綁操作
// 因為 referrer 要和其他對象建立關系了
remove_referrer(entry, referrer);
bool empty = true;
if (entry->out_of_line() && entry->num_refs != 0) {
empty = false;
}
else {
for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
if (entry->inline_referrers[i]) {
empty = false;
break;
}
}
}
if (empty) {
weak_entry_remove(weak_table, entry);
}
}
// Do not set *referrer = nil. objc_storeWeak() requires that the
// value not change.
}
weak 關鍵字修飾的屬性或者變量為什么在對應類實例dealloc后會置為nil,那是因為在類實例釋放的時候,dealloc 會從全局的引用計數和weak計數表sideTables中,通過實例地址去找到屬于自己的那張表,表中的 weak_table->weak_entries 存儲了所有 entry 對象——其實就是所有指向這個實例對象的變量,
weak_entry_t
中的referrers
數組存儲的就是變量或屬性的內存地址,逐一置為nil即可。
關聯對象基本使用方法:
#import <objc/runtime.h>
static NSString * const kKeyOfImageProperty;
@implementation UIView (Image)
- (UIImage *)pt_image {
return objc_getAssociatedObject(self, &kKeyOfImageProperty);
}
- (void)setPTImage:(UIImage *)image {
objc_setAssociatedObject(self, &kKeyOfImageProperty, image,OBJC_ASSOCIATION_RETAIN);
}
@end
objc_AssociationPolicy
關聯對象持有策略有如下幾種 :
Behavior | @property Equivalent | Description |
---|---|---|
OBJC_ASSOCIATION_ASSIGN | @property (assign) 或 @property (unsafe_unretained) | 指定一個關聯對象的弱引用。 |
OBJC_ASSOCIATION_RETAIN_NONATOMIC | @property (nonatomic, strong) | 指定一個關聯對象的強引用,不能被原子化使用。 |
OBJC_ASSOCIATION_COPY_NONATOMIC | @property (nonatomic, copy) | 指定一個關聯對象的copy引用,不能被原子化使用。 |
OBJC_ASSOCIATION_RETAIN | @property (atomic, strong) | 指定一個關聯對象的強引用,能被原子化使用。 |
OBJC_ASSOCIATION_COPY | @property (atomic, copy) | 指定一個關聯對象的copy引用,能被原子化使用。 |
OBJC_ASSOCIATION_GETTER_AUTORELEASE | 自動釋放類型 |
摘自瓜地:OBJC_ASSOCIATION_ASSIGN類型的關聯對象和
weak
有一定差別,而更加接近于unsafe_unretained
,即當目標對象遭到摧毀時,屬性值不會自動清空。(翻譯自Associated Objects)同樣是Associated Objects文中,總結了三個關于Associated Objects用法:
- 為Class添加私有成員:例如在AFNetworking中,在UIImageView里添加了imageRequestOperation對象,從而保證了異步加載圖片。
- 為Class添加共有成員:例如在FDTemplateLayoutCell中,使用Associated Objects來緩存每個cell的高度(代碼片段1、代碼片段2)。通過分配不同的key,在復用cell的時候即時取出,增加效率。
- 創建KVO對象:建議使用category來創建關聯對象作為觀察者??梢詤⒖?a target="_blank">Objective-C Associated Objects這篇文的例子。
源碼實現非常簡單,我添加了完整注釋,對c++語法也做了一定解釋:
id _object_get_associative_reference(id object, void *key) {
id value = nil;
uintptr_t policy = OBJC_ASSOCIATION_ASSIGN;
{
AssociationsManager manager;
// manager.associations() 返回的是一個 `AssociationsHashMap` 對象(*_map)
// 所以這里 `&associations` 中用了 `&`
AssociationsHashMap &associations(manager.associations());
// intptr_t 是為了兼容平臺,在64位的機器上,intptr_t和uintptr_t分別是long int、unsigned long int的別名;在32位的機器上,intptr_t和uintptr_t分別是int、unsigned int的別名
// DISGUISE 內部對指針做了 ~ 取反操作,“偽裝”?
disguised_ptr_t disguised_object = DISGUISE(object);
/*
AssociationsHashMap 繼承自 unordered_map,存儲 key-value 的組合
iterator find ( const key_type& key ),如果 key 存在,則返回key對象的迭代器,
如果key不存在,則find返回 unordered_map::end;因此可以通過 `map.find(key) == map.end()`
判斷 key 是否存在于當前 map 中。
*/
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
/*
unordered_map 的鍵值分別是迭代器的first和second屬性。
所以說上面先通過 object 對象(實例對象or類對象) 找到其所有關聯對象
i->second 取到又是一個 ObjectAssociationMap
此刻再通過我們自己設定的 key 來查找對應的關聯屬性值,不過使用
`ObjcAssociation` 封裝的
*/
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
ObjcAssociation &entry = j->second;
value = entry.value();
policy = entry.policy();
// 如果策略是 getter retain ,注意這里留個坑
// 平常 OBJC_ASSOCIATION_RETAIN = 01401
// OBJC_ASSOCIATION_GETTER_RETAIN = (1 << 8)
if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) {
// TODO: 有學問
objc_retain(value);
}
}
}
}
if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) {
objc_autorelease(value);
}
return value;
}
對應的set操作實現同樣簡單,耐心看下源碼注釋,即使不同c++都沒問題:
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
// retain the new value (if any) outside the lock.
ObjcAssociation old_association(0, nil);
// 如果value對象存在,則進行retain or copy 操作
id new_value = value ? acquireValue(value, policy) : nil;
{
AssociationsManager manager;
// manager.associations() 返回的是一個 `AssociationsHashMap` 對象(*_map)
// 所以這里 `&associations` 中用了 `&`
AssociationsHashMap &associations(manager.associations());
// intptr_t 是為了兼容平臺,在64位的機器上,intptr_t和uintptr_t分別是long int、unsigned long int的別名;在32位的機器上,intptr_t和uintptr_t分別是int、unsigned int的別名
// DISGUISE 內部對指針做了 ~ 取反操作,“偽裝”
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
// break any existing association.
/*
AssociationsHashMap 繼承自 unordered_map,存儲 key-value 的組合
iterator find ( const key_type& key ),如果 key 存在,則返回key對象的迭代器,
如果key不存在,則find返回 unordered_map::end;因此可以通過 `map.find(key) == map.end()`
判斷 key 是否存在于當前 map 中。
*/
AssociationsHashMap::iterator i = associations.find(disguised_object);
// 這里和get操作不同,set操作時如果查詢到對象沒有關聯對象,那么這一次設值是第一次,
// 所以會創建一個新的 ObjectAssociationMap 用來存儲實例對象的所有關聯屬性
if (i != associations.end()) {
// secondary table exists
/*
unordered_map 的鍵值分別是迭代器的first和second屬性。
所以說上面先通過 object 對象(實例對象or類對象) 找到其所有關聯對象
i->second 取到又是一個 ObjectAssociationMap
此刻再通過我們自己設定的 key 來查找對應的關聯屬性值,不過使用
`ObjcAssociation` 封裝的
*/
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
// 關聯屬性用 ObjcAssociation 結構體封裝
if (j != refs->end()) {
old_association = j->second;
j->second = ObjcAssociation(policy, new_value);
} else {
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
// create the new association (first time).
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
(*refs)[key] = ObjcAssociation(policy, new_value);
// 知識點是:newisa.has_assoc = true;
object->setHasAssociatedObjects();
}
} else {
// setting the association to nil breaks the association.
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}
}
}
// release the old value (outside of the lock).
if (old_association.hasValue()) ReleaseValue()(old_association);
}
3. 關聯對象的如何進行內存管理的?關聯對象如何實現weak屬性
使用了 policy
設置內存管理策略,具體見上。
4. Autoreleasepool
的原理?所使用的的數據結構是什么
5. ARC
的實現原理?ARC
下對retain & release
做了哪些優化
6. ARC
下哪些情況會造成內存泄漏
其他
-
Method Swizzle
注意事項 - 屬性修飾符
atomic
的內部實現是怎么樣的?能保證線程安全嗎 - iOS 中內省的幾個方法有哪些?內部實現原理是什么
-
class、objc_getClass、object_getclass
方法有什么區別?
NSNotification相關
認真研讀、你可以在這里找到答案輕松過面:一文全解iOS通知機制(經典收藏)
- 實現原理(結構設計、通知如何存儲的、
name&observer&SEL
之間的關系等) - 通知的發送時同步的,還是異步的
-
NSNotificationCenter
接受消息和發送消息是在一個線程里嗎?如何異步發送消息 -
NSNotificationQueue
是異步還是同步發送?在哪個線程響應 -
NSNotificationQueue
和runloop
的關系 - 如何保證通知接收的線程在主線程
- 頁面銷毀時不移除通知會崩潰嗎
- 多次添加同一個通知會是什么結果?多次移除通知呢
- 下面的方式能接收到通知嗎?為什么
// 發送通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"TestNotification" object:@1];
// 接收通知
[NSNotificationCenter.defaultCenter postNotificationName:@"TestNotification" object:nil];
復制代碼
Runloop & KVO
runloop
runloop
對于一個標準的iOS開發來說都不陌生,應該說熟悉runloop
是標配,下面就隨便列幾個典型問題吧
- app如何接收到觸摸事件的
- 為什么只有主線程的
runloop
是開啟的 - 為什么只在主線程刷新UI
-
PerformSelector
和runloop
的關系 - 如何使線程保活
KVO(Finished)
同runloop
一樣,這也是標配的知識點了,同樣列出幾個典型問題
1. 實現原理
KVO 會為需要observed的對象動態創建一個子類,以NSKVONotifying_
最為前綴,然后將對象的 isa 指針指向新的子類,同時重寫 class 方法,返回原先類對象,這樣外部就無感知了;其次重寫所有要觀察屬性的setter方法,統一會走一個方法,然后內部是會調用 willChangeValueForKey
和 didChangevlueForKey
方法,在一個被觀察屬性發生改變之前, willChangeValueForKey:
一定會被調用,這就 會記錄舊的值。而當改變發生后,didChangeValueForKey:
會被調用,繼而 observeValueForKey:ofObject:change:context:
也會被調用。
那么如何驗證上面的說法呢?很簡單,借助runtime 即可,測試代碼請點擊這里:
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] initWithName:@"pmst" age:18];
self.teacher = [[Teacher alloc] initWithName:@"ppp" age:28];
self.teacher.work = @"數學";
self.teacher.numberOfStudent = 10;
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
RuntimeUtil *utils = [RuntimeUtil new];
[utils logClassInfo:self.person.class];
[self.person addObserver:self forKeyPath:@"age" options:options context:nil];
[utils logClassInfo:object_getClass(self.person)];
[utils logClassInfo:self.teacher.class];
[self.teacher addObserver:self forKeyPath:@"age" options:options context:nil];
[self.teacher addObserver:self forKeyPath:@"name" options:options context:nil];
[self.teacher addObserver:self forKeyPath:@"work" options:options context:nil];
[utils logClassInfo:object_getClass(self.teacher)];
}
這里 object_getClass()
方法實現也貼一下,如果直接使用 .class
那么因為被重寫過,返回的還是原先對象的類對象,而直接用 runtime 方法的直接返回了 isa
指針。
Class object_getClass(id obj)
{
if (obj) return obj->getIsa();
else return Nil;
}
通過日志確實可以看到子類重寫了對應屬性的setter方法:
2020-03-25 23:11:00.607820+0800 02-25-KVO[28370:1005147] LOG:(NSKVONotifying_Teacher) INFO
2020-03-25 23:11:00.608190+0800 02-25-KVO[28370:1005147] ==== OUTPUT:NSKVONotifying_Teacher properties ====
2020-03-25 23:11:00.608529+0800 02-25-KVO[28370:1005147] ==== OUTPUT:NSKVONotifying_Teacher Method ====
2020-03-25 23:11:00.608876+0800 02-25-KVO[28370:1005147] method name:setWork:
2020-03-25 23:11:00.609219+0800 02-25-KVO[28370:1005147] method name:setName:
2020-03-25 23:11:00.646713+0800 02-25-KVO[28370:1005147] method name:setAge:
2020-03-25 23:11:00.646858+0800 02-25-KVO[28370:1005147] method name:class
2020-03-25 23:11:00.646971+0800 02-25-KVO[28370:1005147] method name:dealloc
2020-03-25 23:11:00.647088+0800 02-25-KVO[28370:1005147] method name:_isKVOA
2020-03-25 23:11:00.647207+0800 02-25-KVO[28370:1005147] =========================
疑惑點:看到有文章提出 KVO 之后,setXXX 方法轉而調用
_NSSetBoolValueAndNotify、_NSSetCharValueAndNotify、_NSSetFloatValueAndNotify、_NSSetLongValueAndNotify
等方法,但是通過 runtime 打印 method 是存在的,猜測 SEL 是一樣的,但是 IMP 被換掉了,關于源碼的實現還未找到。TODO下。
2. 如何手動關閉kvo
KVO 和 KVC 相關接口太多,實際開發中直接查看接口文檔即可。
+(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{
if ([key isEqualToString:@"name"]) {
return NO;
}else{
return [super automaticallyNotifiesObserversForKey:key];
}
}
-(void)setName:(NSString *)name{
if (_name!=name) {
[self willChangeValueForKey:@"name"];
_name=name;
[self didChangeValueForKey:@"name"];
}
}
3. 通過KVC修改屬性會觸發KVO么
會觸發 KVO 操作,KVC 時候會先查詢對應的 getter 和 setter 方法,如果都沒找到,調用
+ (BOOL)accessInstanceVariablesDirectly {
return NO;
}
如果返回 YES,那么可以直接修改實例變量。
KVC 調用 getter 流程:
getKEY,KEY,isKEY, _KEY
,接著是實例變量_KEY,_isKEY, KEY, isKEY
;KVC 調用 setter 流程:
setKEY
和_setKEY
,實例變量順序_KEY,_isKEY, KEY, isKEY
,沒找到就調用setValue: forUndefinedKey:
4. 哪些情況下使用kvo會崩潰,怎么防護崩潰
- dealloc 沒有移除 kvo 觀察者,解決方案:創建一個中間對象,將其作為某個屬性的觀察者,然后dealloc的時候去做移除觀察者,而調用者是持有中間對象的,調用者釋放了,中間對象也釋放了,dealloc 也就移除觀察者了;
- 多次重復移除同一個屬性,移除了未注冊的觀察者
- 被觀察者提前被釋放,被觀察者在 dealloc 時仍然注冊著 KVO,導致崩潰。 例如:被觀察者是局部變量的情況(iOS 10 及之前會崩潰) 比如 weak ;
- 添加了觀察者,但未實現
observeValueForKeyPath:ofObject:change:context:
方法,導致崩潰; - 添加或者移除時
keypath == nil
,導致崩潰;
以下解決方案出自 iOS 開發:『Crash 防護系統』(二)KVO 防護 一文。
解決方案一:
FBKVOController 對 KVO 機制進行了額外的一層封裝,框架不但可以自動幫我們移除觀察者,還提供了 block 或者 selector 的方式供我們進行觀察處理。不可否認的是,FBKVOController 為我們的開發提供了很大的便利性。但是相對而言,這種方式對項目代碼的侵入性比較大,必須依靠編碼規范來強制約束團隊人員使用這種方式。
解決方案二:
首先為 NSObject 建立一個分類,利用 Method Swizzling,實現自定義的
BMP_addObserver:forKeyPath:options:context:
、BMP_removeObserver:forKeyPath:
、BMP_removeObserver:forKeyPath:context:
、BMPKVO_dealloc
方法,用來替換系統原生的添加移除觀察者方法的實現。然后在觀察者和被觀察者之間建立一個
KVODelegate 對象
,兩者之間通過KVODelegate 對象
建立聯系。然后在添加和移除操作時,將 KVO 的相關信息例如observer
、keyPath
、options
、context
保存為KVOInfo 對象
,并添加到KVODelegate 對象
中對應 的關系哈希表
中,對應原有的添加觀察者。 關系哈希表的數據結構:{keypath : [KVOInfo 對象1, KVOInfo 對象2, ... ]}
在添加和移除操作的時候,利用
KVODelegate 對象
做轉發,把真正的觀察者變為KVODelegate 對象
,而當被觀察者的特定屬性發生了改變,再由KVODelegate 對象
分發到原有的觀察者上。添加觀察者時:通過關系哈希表判斷是否重復添加,只添加一次。
移除觀察者時:通過關系哈希表是否已經進行過移除操作,避免多次移除。
觀察鍵值改變時:同樣通過關系哈希表判斷,將改變操作分發到原有的觀察者上。
解決方案三:
XXShield 實現方案和 BayMax 系統類似。也是利用一個 Proxy 對象用來做轉發, 真正的觀察者是 Proxy,被觀察者出現了通知信息,由 Proxy 做分發。不過不同點是 Proxy 里面保存的內容沒有前者多。只保存了 _observed(被觀察者)
和關系哈希表,這個關系哈希表中只維護了 keyPath
和 observer
的關系。
關系哈希表的數據結構:{keypath : [observer1, observer2 , ...](NSHashTable)}
。
XXShield 在 dealloc 中也做了類似將多余觀察者移除掉的操作,是通過關系數據結構和 _observed
,然后調用原生移除觀察者操作實現的。
5. kvo的優缺點
優點:
- 運用了設計模式:觀察者模式
- 支持多個觀察者觀察同一屬性,或者一個觀察者監聽不同屬性。
- 開發人員不需要實現屬性值變化了發送通知的方案,系統已經封裝好了,大大減少開發工作量;
- 能夠對非我們創建的對象,即內部對象的狀態改變作出響應,而且不需要改變內部對象(SDK對象)的實現;
- 能夠提供觀察的屬性的最新值以及先前值;
- 用key paths來觀察屬性,因此也可以觀察嵌套對象;
- 完成了對觀察對象的抽象,因為不需要額外的代碼來允許觀察值能夠被觀察
缺點:
- 觀察的屬性鍵值硬編碼(字符串),編譯器不會出現警告以及檢查;
- 由于允許對一個對象進行不同屬性觀察,所以在唯一回調方法中,會出現地獄式
if-else if - else
分支處理情況;
References:
Block
-
block
的內部實現,結構體是什么樣的 - block是類嗎,有哪些類型
- 一個
int
變量被__block
修飾與否的區別?block的變量截獲 -
block
在修改NSMutableArray
,需不需要添加__block
- 怎么進行內存管理的
-
block
可以用strong
修飾嗎 - 解決循環引用時為什么要用
__strong、__weak
修飾 -
block
發生copy
時機 -
Block
訪問對象類型的auto變量
時,在ARC和MRC
下有什么區別
多線程
主要以GCD為主
-
iOS
開發中有多少類型的線程?分別對比 -
GCD
有哪些隊列,默認提供哪些隊列 -
GCD
有哪些方法api -
GCD
主線程 & 主隊列的關系 - 如何實現同步,有多少方式就說多少
-
dispatch_once
實現原理 - 什么情況下會死鎖
- 有哪些類型的線程鎖,分別介紹下作用和使用場景
-
NSOperationQueue
中的maxConcurrentOperationCount
默認值 -
NSTimer、CADisplayLink、dispatch_source_t
的優劣
視圖&圖像相關
-
AutoLayout
的原理,性能如何 -
UIView & CALayer
的區別 - 事件響應鏈
-
drawrect & layoutsubviews
調用時機 - UI的刷新原理
- 隱式動畫 & 顯示動畫區別
- 什么是離屏渲染
- imageName & imageWithContentsOfFile區別
- 多個相同的圖片,會重復加載嗎
- 圖片是什么時候解碼的,如何優化
- 圖片渲染怎么優化
- 如果GPU的刷新率超過了iOS屏幕60Hz刷新率是什么現象,怎么解決
性能優化
- 如何做啟動優化,如何監控
- 如何做卡頓優化,如何監控
- 如何做耗電優化,如何監控
- 如何做網絡優化,如何監控
開發證書
- 蘋果使用證書的目的是什么
- AppStore安裝app時的認證流程
- 開發者怎么在debug模式下把app安裝到設備呢
架構設計
典型源碼的學習
只是列出一些iOS比較核心的開源庫,這些庫包含了很多高質量的思想,源碼學習的時候一定要關注每個框架解決的核心問題是什么,還有它們的優缺點,這樣才能算真正理解和吸收
- AFN
- SDWebImage
- JSPatch、Aspects(雖然一個不可用、另一個不維護,但是這兩個庫都很精煉巧妙,很適合學習)
- Weex/RN, 筆者認為這種前端和客戶端緊密聯系的庫是必須要知道其原理的
- CTMediator、其他router庫,這些都是常見的路由庫,開發中基本上都會用到
- 請
圈友
們在評論下面補充吧
架構設計
- 手動埋點、自動化埋點、可視化埋點
-
MVC、MVP、MVVM
設計模式 - 常見的設計模式
- 單例的弊端
- 常見的路由方案,以及優缺點對比
- 如果保證項目的穩定性
- 設計一個圖片緩存框架(LRU)
- 如何設計一個
git diff
- 設計一個線程池?畫出你的架構圖
- 你的app架構是什么,有什么優缺點、為什么這么做、怎么改進
其他問題
-
PerformSelector & NSInvocation
優劣對比 -
oc
怎么實現多繼承?怎么面向切面(可以參考Aspects深度解析-iOS面向切面編程) - 哪些
bug
會導致崩潰,如何防護崩潰 - 怎么監控崩潰
-
app
的啟動過程(考察LLVM編譯過程、靜態鏈接、動態鏈接、runtime初始化) - 沙盒目錄的每個文件夾劃分的作用
- 簡述下
match-o
文件結構
系統基礎知識
- 進程和線程的區別
-
HTTPS
的握手過程 - 什么是
中間人攻擊
?怎么預防 -
TCP
的握手過程?為什么進行三次握手,四次揮手 -
堆和棧
區的區別?誰的占用內存空間大 - 加密算法:
對稱加密算法和非對稱加密算法
區別 - 常見的
對稱加密和非對稱加密
算法有哪些 -
MD5、Sha1、Sha256
區別 -
charles
抓包過程?不使用charles
,4G
網絡如何抓包
數據結構與算法
對于移動開發者來說,一般不會遇到非常難的算法,大多以數據結構為主,筆者列出一些必會的算法,當然有時間了可以去LeetCode上刷刷題
- 八大排序算法
- 棧&隊列
- 字符串處理
- 鏈表
- 二叉樹相關操作
- 深搜廣搜
- 基本的動態規劃題、貪心算法、二分查找