SlideShare a Scribd company logo
1 of 42
Download to read offline
Objective C Runtime
Khoa Pham
2359Media
Today
● self = [super init]
● objc_msgSend
● ObjC Runtime
● How to use the Runtime API
● Use cases
self = [super init]
ETPAnimal *cat = [ETPAnimal cat];
NSInteger recordCount = [ETPCoreDataManager
recordCount];
self = [super init]
@interface ETPCat : ETPAnimal
@end
ETPCat *cat = [[ETPCat alloc] init]
self = [super init]
- (id)init
{
self = [super init];
if (self) {
// ETPCat does it own initialization here
}
return self;
}
self = [super init]
[super init] calls the superclass implementation of init with
the (hidden) self argument.
It can do one of these things
+ set some properties on self, and return self
+ return a different object (factory, singleton)
+ return nil
self = [super init]
ETPCat *cat = [ETPCat alloc] // 0x1111111a
[cat init] // 0x1111111b
[cat meomeo] // 0x1111111a
self = [super init]
Demo
objc_msgSend
ETPCat *cat = [[ETPCat alloc] init]
[cat setName:@”meo”]
objc_msgSend(cat, @selector(setName:), @”meo”)
objc_msgSend
Demo
objc_msgSend
@selector
SEL selector1 = @selector(initWithName:)
SEL selector2 = @selector(initWithFriends1Name::)
typedef struct objc_selector *SEL
Read more at Objective C Runtime Reference -> Data
Structure -> Class definition Data structure -> SEL
@selector
Demo
Objective C Runtime
The Objective-C Runtime is a Runtime Library, it's a library
written mainly in C & Assembler that adds the Object
Oriented capabilities to C to create Objective-C.
Objective C Runtime
Source code http://www.opensource.apple.
com/source/objc4/objc4-532/runtime/objc-class.mm
There are two versions of the Objective-C runtime—
“modern” and “legacy”. The modern version was introduced
with Objective-C 2.0 and includes a number of new
features.
Objective C Runtime
Dynamic feature
Object oriented capability
Objective C Runtime
Features
● Class elements (categories, methods, variables,
property, …)
● Object
● Messaging
● Object introspection
Objective C Runtime
@interface ETPAnimal : NSObject
@end
typedef struct objc_class *Class;
Objective C Runtime (old)
struct objc_class {
Class isa;
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
}
Objective C Runtime
typedef struct class_ro_t
{
const char * name;
const ivar_list_t * ivars;
} class_ro_t
typedef struct class_rw_t
{
const class_ro_t *ro;
method_list_t **methods;
struct class_t *firstSubclass;
struct class_t *nextSiblingClass;
} class_rw_t;
Objective C Runtime
ETPAnimal *animal = [[ETPAnimal alloc] init]
struct objc_object
{
Class isa;
// variables
};
Objective C Runtime
id someAnimal = [[ETPAnimal alloc] init]
typedef struct objc_object
{
Class isa;
} *id;
Objective C Runtime
Class is also an object, its isa pointer points to its meta
class
The metaclass is the description of the class object
Objective C Runtime
Objective C Runtime
Demo
Objective C Runtime
● Dynamic typing
● Dynamic binding
● Dynamic method resolution
● Introspection
Objective C Runtime
Dynamic typing
Dynamic typing enables the runtime to determine the type
of an object at runtime
id cat = [[ETPCat alloc] init]
- (void)acceptAnything:(id)anything;
Objective C Runtime
Dynamic binding
Dynamic binding is the process of mapping a message to a
method at runtime, rather than at compile time
Objective C Runtime
Dynamic method resolution
Provide the implementation of a method dynamically.
@dynamic
Objective C Runtime
Introspection
isKindOfClass
respondsToSelector
conformsToProtocol
How to use the Runtime API
Objective-C programs interact with the runtime system to
implement the dynamic features of the language.
● Objective-C source code
● Foundation Framework NSObject methods
● Runtime library API
Use cases
Method swizzle (IIViewDeckController)
JSON Model (Torin ‘s BaseModel)
Message forwarding
Meta programming
Use cases
Method swizzle
Use cases
Method swizzle (IIViewDeckController)
SEL presentVC = @selector(presentViewController:animated:completion:);
SEL vdcPresentVC = @selector(vdc_presentViewController:animated:completion:);
method_exchangeImplementations(class_getInstanceMethod(self, presentVC),
class_getInstanceMethod(self, vdcPresentVC));
Use cases
Method swizzle (IIViewDeckController)
- (void)vdc_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)
animated completion:(void (^)(void))completion {
UIViewController* controller = self.viewDeckController ?: self;
[controller vdc_presentViewController:viewControllerToPresent animated:animated completion:
completion]; // when we get here, the vdc_ method is actually the old, real method
}
Use cases
JSON Model (Torin ‘s BaseModel)
updateWithDictionary
class_copyIvarList
ivar_getName
Use cases
JSON Model (Torin ‘s BaseModel)
@interface ETPItem : BaseModel
@property (nonatomic, copy) NSString * ID;
@property (nonatomic, copy) NSString *name;
@end
ETPItem *item = [[ETPItem alloc] init];
[item updateWithDictionary:@{@”ID”: @”1”, @”name”: @”item1”}];
Message forwarding
Use cases
Meta programming
● Dynamic method naming
● Validation
● Template
● Mocking
Reference
1. http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html
2. http://www.slideshare.net/mudphone/what-makes-objective-c-dynamic
3. http://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html
4. http://nshipster.com/method-swizzling/
5. http://stackoverflow.com/questions/415452/object-orientation-in-c
6. http://stackoverflow.com/questions/2766233/what-is-the-c-runtime-library
7. http://gcc.gnu.org/onlinedocs/gcc/Modern-GNU-Objective-C-runtime-API.html
8. http://www.opensource.apple.com/source/objc4/objc4-532/runtime/objc-class.mm
9. http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/
10. https://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html
11. Pro Objective C, chapter 7, 8, 9
12. Effective Objective C, chapter 2
13. http://wiki.gnustep.org/index.php/ObjC2_FAQ
Thank you
Q&A

More Related Content

What's hot

GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? ICS
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threadsYnon Perek
 
Java 14 features
Java 14 featuresJava 14 features
Java 14 featuresAditi Anand
 
Native code in Android applications
Native code in Android applicationsNative code in Android applications
Native code in Android applicationsDmitry Matyukhin
 
C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linuxMiller Lee
 
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkZachary Blair
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building BlocksMax Kleiner
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...ICS
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)chan20kaur
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?Kyle Oba
 

What's hot (20)

GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
Progress_190412
Progress_190412Progress_190412
Progress_190412
 
Vulkan 1.1 Reference Guide
Vulkan 1.1 Reference GuideVulkan 1.1 Reference Guide
Vulkan 1.1 Reference Guide
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
Runtime
RuntimeRuntime
Runtime
 
Java 14 features
Java 14 featuresJava 14 features
Java 14 features
 
Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
 
Native code in Android applications
Native code in Android applicationsNative code in Android applications
Native code in Android applications
 
C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linux
 
OpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick ReferenceOpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick Reference
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application Framework
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building Blocks
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Android JNI
Android JNIAndroid JNI
Android JNI
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 

Similar to Objective-C Runtime overview

Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...Yandex
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorialantiw
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to DistributionTunvir Rahman Tusher
 
How to instantiate any view controller for free
How to instantiate any view controller for freeHow to instantiate any view controller for free
How to instantiate any view controller for freeBenotCaron
 
Daggerate your code - Write your own annotation processor
Daggerate your code - Write your own annotation processorDaggerate your code - Write your own annotation processor
Daggerate your code - Write your own annotation processorBartosz Kosarzycki
 
Java programming concept
Java programming conceptJava programming concept
Java programming conceptSanjay Gunjal
 
The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84Mahmoud Samir Fayed
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 
How to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveHow to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveAmiq Consulting
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-Ccorehard_by
 
The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196Mahmoud Samir Fayed
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The LandingHaci Murat Yaman
 
The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202Mahmoud Samir Fayed
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-CKazunobu Tasaka
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsMatteo Manchi
 
Suite Script 2.0 API Basics
Suite Script 2.0 API BasicsSuite Script 2.0 API Basics
Suite Script 2.0 API BasicsJimmy Butare
 
iOS overview
iOS overviewiOS overview
iOS overviewgupta25
 

Similar to Objective-C Runtime overview (20)

Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorial
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to Distribution
 
How to instantiate any view controller for free
How to instantiate any view controller for freeHow to instantiate any view controller for free
How to instantiate any view controller for free
 
Daggerate your code - Write your own annotation processor
Daggerate your code - Write your own annotation processorDaggerate your code - Write your own annotation processor
Daggerate your code - Write your own annotation processor
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84The Ring programming language version 1.2 book - Part 51 of 84
The Ring programming language version 1.2 book - Part 51 of 84
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
MattsonTutorialSC14.pdf
MattsonTutorialSC14.pdfMattsonTutorialSC14.pdf
MattsonTutorialSC14.pdf
 
How to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveHow to Connect SystemVerilog with Octave
How to Connect SystemVerilog with Octave
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-C
 
The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The Landing
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
Suite Script 2.0 API Basics
Suite Script 2.0 API BasicsSuite Script 2.0 API Basics
Suite Script 2.0 API Basics
 
iOS overview
iOS overviewiOS overview
iOS overview
 

Recently uploaded

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

Objective-C Runtime overview

  • 1. Objective C Runtime Khoa Pham 2359Media
  • 2. Today ● self = [super init] ● objc_msgSend ● ObjC Runtime ● How to use the Runtime API ● Use cases
  • 3. self = [super init] ETPAnimal *cat = [ETPAnimal cat]; NSInteger recordCount = [ETPCoreDataManager recordCount];
  • 4. self = [super init] @interface ETPCat : ETPAnimal @end ETPCat *cat = [[ETPCat alloc] init]
  • 5. self = [super init] - (id)init { self = [super init]; if (self) { // ETPCat does it own initialization here } return self; }
  • 6. self = [super init] [super init] calls the superclass implementation of init with the (hidden) self argument. It can do one of these things + set some properties on self, and return self + return a different object (factory, singleton) + return nil
  • 7. self = [super init] ETPCat *cat = [ETPCat alloc] // 0x1111111a [cat init] // 0x1111111b [cat meomeo] // 0x1111111a
  • 8. self = [super init] Demo
  • 9. objc_msgSend ETPCat *cat = [[ETPCat alloc] init] [cat setName:@”meo”] objc_msgSend(cat, @selector(setName:), @”meo”)
  • 12. @selector SEL selector1 = @selector(initWithName:) SEL selector2 = @selector(initWithFriends1Name::) typedef struct objc_selector *SEL Read more at Objective C Runtime Reference -> Data Structure -> Class definition Data structure -> SEL
  • 14. Objective C Runtime The Objective-C Runtime is a Runtime Library, it's a library written mainly in C & Assembler that adds the Object Oriented capabilities to C to create Objective-C.
  • 15. Objective C Runtime Source code http://www.opensource.apple. com/source/objc4/objc4-532/runtime/objc-class.mm There are two versions of the Objective-C runtime— “modern” and “legacy”. The modern version was introduced with Objective-C 2.0 and includes a number of new features.
  • 16. Objective C Runtime Dynamic feature Object oriented capability
  • 17. Objective C Runtime Features ● Class elements (categories, methods, variables, property, …) ● Object ● Messaging ● Object introspection
  • 18. Objective C Runtime @interface ETPAnimal : NSObject @end typedef struct objc_class *Class;
  • 19. Objective C Runtime (old) struct objc_class { Class isa; Class super_class OBJC2_UNAVAILABLE; const char *name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; struct objc_method_list **methodLists OBJC2_UNAVAILABLE; struct objc_cache *cache OBJC2_UNAVAILABLE; struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; }
  • 20. Objective C Runtime typedef struct class_ro_t { const char * name; const ivar_list_t * ivars; } class_ro_t typedef struct class_rw_t { const class_ro_t *ro; method_list_t **methods; struct class_t *firstSubclass; struct class_t *nextSiblingClass; } class_rw_t;
  • 21. Objective C Runtime ETPAnimal *animal = [[ETPAnimal alloc] init] struct objc_object { Class isa; // variables };
  • 22. Objective C Runtime id someAnimal = [[ETPAnimal alloc] init] typedef struct objc_object { Class isa; } *id;
  • 23. Objective C Runtime Class is also an object, its isa pointer points to its meta class The metaclass is the description of the class object
  • 25.
  • 27. Objective C Runtime ● Dynamic typing ● Dynamic binding ● Dynamic method resolution ● Introspection
  • 28. Objective C Runtime Dynamic typing Dynamic typing enables the runtime to determine the type of an object at runtime id cat = [[ETPCat alloc] init] - (void)acceptAnything:(id)anything;
  • 29. Objective C Runtime Dynamic binding Dynamic binding is the process of mapping a message to a method at runtime, rather than at compile time
  • 30. Objective C Runtime Dynamic method resolution Provide the implementation of a method dynamically. @dynamic
  • 32. How to use the Runtime API Objective-C programs interact with the runtime system to implement the dynamic features of the language. ● Objective-C source code ● Foundation Framework NSObject methods ● Runtime library API
  • 33. Use cases Method swizzle (IIViewDeckController) JSON Model (Torin ‘s BaseModel) Message forwarding Meta programming
  • 35. Use cases Method swizzle (IIViewDeckController) SEL presentVC = @selector(presentViewController:animated:completion:); SEL vdcPresentVC = @selector(vdc_presentViewController:animated:completion:); method_exchangeImplementations(class_getInstanceMethod(self, presentVC), class_getInstanceMethod(self, vdcPresentVC));
  • 36. Use cases Method swizzle (IIViewDeckController) - (void)vdc_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL) animated completion:(void (^)(void))completion { UIViewController* controller = self.viewDeckController ?: self; [controller vdc_presentViewController:viewControllerToPresent animated:animated completion: completion]; // when we get here, the vdc_ method is actually the old, real method }
  • 37. Use cases JSON Model (Torin ‘s BaseModel) updateWithDictionary class_copyIvarList ivar_getName
  • 38. Use cases JSON Model (Torin ‘s BaseModel) @interface ETPItem : BaseModel @property (nonatomic, copy) NSString * ID; @property (nonatomic, copy) NSString *name; @end ETPItem *item = [[ETPItem alloc] init]; [item updateWithDictionary:@{@”ID”: @”1”, @”name”: @”item1”}];
  • 40. Use cases Meta programming ● Dynamic method naming ● Validation ● Template ● Mocking
  • 41. Reference 1. http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html 2. http://www.slideshare.net/mudphone/what-makes-objective-c-dynamic 3. http://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html 4. http://nshipster.com/method-swizzling/ 5. http://stackoverflow.com/questions/415452/object-orientation-in-c 6. http://stackoverflow.com/questions/2766233/what-is-the-c-runtime-library 7. http://gcc.gnu.org/onlinedocs/gcc/Modern-GNU-Objective-C-runtime-API.html 8. http://www.opensource.apple.com/source/objc4/objc4-532/runtime/objc-class.mm 9. http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/ 10. https://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html 11. Pro Objective C, chapter 7, 8, 9 12. Effective Objective C, chapter 2 13. http://wiki.gnustep.org/index.php/ObjC2_FAQ