iOS Runtime《二》 分类Category 相关

 

1、给分类绑定对象

objc_AssociationPolicy 是一个枚举,里面的枚举值分别代表要添加的属性的修饰类型。

1、  OBJC_ASSOCIATION_ASSIGN                            相当于weak

2 、 OBJC_ASSOCIATION_RETAIN_NONATOMIC    相当于strongnonatomic

3、  OBJC_ASSOCIATION_COPY_NONATOMIC        相当于copynonatomic                          

4、  OBJC_ASSOCIATION_RETAIN                             相当于strongatomic

5、  OBJC_ASSOCIATION_COPY                               相当于copyatomic

 

2、方法:

//1、 设置关联对象

OBJC_EXPORT void
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

//2、获得关联对象

OBJC_EXPORT id _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

//3、删除关联对象。

OBJC_EXPORT void
objc_removeAssociatedObjects(id _Nonnull object)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

例:

// 1、给 NSObject  添加一个分类,分类添加一个cName属性
// .h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (AA)

@property (nonatomic,copy)NSString *cName;

@end

NS_ASSUME_NONNULL_END

//.m
#import "NSObject+AA.h"
#import <objc/message.h>

@implementation NSObject (AA)

-(void)setCName:(NSString *)cName
{
    objc_setAssociatedObject(self, "cName", cName, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)cName
{
   return  objc_getAssociatedObject(self, "cName");
}

@end

// 3、使用
 NSObject *obj=[[NSObject alloc]init];
    obj.cName=@"哈哈";
    NSLog(@"%@",obj.cName);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: iOS Recipes (第1版) 原名: iOS Recipes: Tips and Tricks for Awesome iPhone and iPad Apps 作者: Paul Warren Matt Drance 资源格式: PDF 版本: 英文文字版/更新EPUB版本/更新源代码 出版社: Pragmatic Bookshelf书号: 978-1-93435-674-6发行时间: 2011年07月07日 地区: 美国 语言: 英文 简介: 内容介绍: Take your iPhone and iPad apps to the next level. You’ve seen cool features and tricks in other apps, but haven’t had the time to really look into how they’re done. We’ve got the answers for you. This book walks you through clean, reusable solutions to a wide variety of problems and patterns common to iOS development with Cocoa Touch and Objective-C. With these recipes in your arsenal, your next apps will be more polished and more maintainable than ever. 目录: Basic UI Recipes Add a Basic Splash Screen Transition Stylize Your Splash Screen Transition Animate a Custom Notification View Create Reusable Toggle Buttons Form Rounded Views with Textured Colors Put Together a Reusable Web View Customize Sliders and Progress Views Shape a Custom Gesture Recognizer Create Self-contained Alert Views Make a Label for Attributed Strings Scroll an Infinite Wall of Album Art excerpt Play Tracks from a Wall of Album Art Have Fun with Autoscrolling Text Views Create a Custom Number Control Table and Scroll View Recipes Simplify Table Cell Production excerpt Use Smart Table Cells in a Nib Locate Table Cell Subviews Organize Complex Table Views Produce Two-Tone Table Views Add Border Shadows for Table Views Place Static Content in a Zoomable Scroll View Build a Carousel Paging Scroll View Graphics Recipes Draw Gradient Filled Bezier Paths Create Dynamic Images With Multiple Animations Make Composited and Transformed Views Animate a Gradient Layer Reshape Shadows Display Animated Views Construct a Simple Emitter Curl the Page to a New View Networking Recipes Tame the Network Activity Indicator excerpt Simplify Web Service Connections Format a Simple HTTP POST Upload Files Over HTTP Runtime Recipes Leverage Modern Objective-C Class Design Produce Intelligent Debug Output Design Smarter User Defaults Access Scan and Traverse View Hierarchies Initialize a Basic Data Model Store Data in a Category
iOS Runtime是一种运行时环境,它是iOS操作系统中的重要组成部分。iOS Runtime为开发者提供了一套动态询问、修改和扩展应用程序行为的机制。 首先,iOS Runtime实现了Objective-C语言的动态特性。Objective-C是一种面向对象的编程语言,它具有动态特性,即在运行时能够动态地修改对象的行为。iOS Runtime允许开发者通过运行时系统对类、对象、方法以及属性进行动态操作。例如,开发者可以在运行时为某个类添加新的方法,也可以通过运行时修改类的实例变量。 其次,iOS Runtime还提供了消息发送机制。在Objective-C中,对象之间的通信是通过消息发送来完成的。iOS Runtime负责将消息转发给正确的接收者,并执行相应的方法。这个过程是动态的,开发者可以在运行时动态改变消息的接收者或方法的实现。这为Objective-C语言带来了很高的灵活性。 此外,iOS Runtime还支持方法的交换与替换。开发者可以通过运行时机制,在运行时将一个方法的实现替换为另一个方法的实现,或者交换两个方法的实现。这对于调试、性能监控和代码扩展都是非常有用的。 总结来说,iOS RuntimeiOS操作系统中实现Objective-C语言特性的核心组件。它提供了动态特性的支持,使开发者能够在运行时动态地修改类、对象和方法的行为,实现更加灵活和可扩展的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值