首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏浮躁的喧嚣

    接口与API设计--22:NSCopying协议

    和mutableCopy拷贝出来的对象中的内容和以前内容一致 2、可变的字符串通过copy操作,生成新的对象 3、可变的字符串通过mutableCopy操作,生成新的可变对象 自定义类的实现copy(NSCopying 协议) 若想令自己写的类具有copy功能,则需要实现NSCopying、NSMutableCopying协议 - (id)copyWithZone:(nullable NSZone *)zone; - mutableCopyWithZone:(nullable NSZone *)zone; 看个例子 #import <Foundation/Foundation.h> @interface Phone : NSObject<NSCopying

    38110编辑于 2023-11-22
  • 来自专栏iOS技术杂谈

    NSCopying和NSCoding对象序列化反序列化基础详解你要知道的NSCopying、NSCoding协议及对象序列化和反序列化都在这里

    你要知道的NSCopying、NSCoding协议及对象序列化和反序列化都在这里 转载请注明出处 https://cloud.tencent.com/developer/user/1605429 本篇文章主要讲解 NSCopying协议,以及NSCoding协议实现对象的序列化和反序列化,实际开发中如果要自己造轮子这两个协议还是比较重要的。 NSCopying协议 Foundation框架中为我们提供的基础的类基本都实现了NSCopying协议,因此,我们可以使用copy方法用来获取对象的一个不可变副本对象,可以使用mutableCopy方法用来获取对象的一个可变副本对象 ,当需要对自定义类调用copy或mutableCopy方法就需要实现NSCopying协议。 对于NSCopying协议,需要注意的就是可变对象和不可变对象之间的区别,以及父类有没有实现NSCopying协议来确定调用何种方法来创建新对象。

    1.7K90发布于 2018-04-10
  • 来自专栏sweet_iOS

    探寻iOS之协议(protocol)

    在iOS开发中,Protocol是一种经常用到的设计模式,苹果的系统框架中也普遍用到了这种方式,比如UITableView中的<UITableViewDelegate>,以及<NSCopying>、<NSObject 重要协议 下面介绍几个重要的系统定义的协议NSObject协议、NSCopying协议、NSMutableCopying协议。 如果想让一个类的对象支持拷贝,就需要让该类实现NSCopying协议。NSCopying协议中的声明的方法只有一个- (id)copyWithZone:(NSZone *)zone。 当我们的类实现了NSCopying协议,通过类的对象调用copy方法时,copy方法就会去调用我们实现的- (id)copyWithZone:(NSZone *)zone方法,实现拷贝功能。 NSCopying协议与NSMutableCopying的区别主要是在于,返回的对象是否是可变类型的。

    1.9K20发布于 2020-12-23
  • 来自专栏Helloted

    Objective-C Copy

    一、NSObject的copy实现 苹果官方文档对copy的描述 NSObject进行拷贝的方法是调用copy方法 newobj = [obj copy] 前提是实现NSCopying协议的copyWithZone **unrecognized selector sent to instance** NSObject does not itself support the NSCopying protocol. NSObject 本身并不支持 NSCopying 协议. 子类必须遵从协议并且实现 copyWithZone: 方法. 如果一个对象的类采用了NSCopying协议并且实现了它的copyWithZone:方法,那么这个对象就可以被拷贝。 这些消息导致调用适当的NSCopying或NSMutableCopying方法。 Copies of objects can be shallow or deep.

    79920编辑于 2022-06-07
  • 来自专栏一“技”之长

    iOS中第三方有序字典框架——M13OrderedDictionary

    lastObject; //获取字典中最后一个元素的键 @property (nonatomic, readonly, M13_NULLABLE) M13GenericType(KeyType, id<NSCopying //获取某个键对应的值 - (M13_NULLABLE M13GenericType(ObjectType, id))objectForKey:(M13GenericType(KeyType, id<NSCopying inRange:(NSRange)range; //查找获取与某个元素的值相同的元素的键 在某个范围内进行查找 - (M13_NULLABLE M13GenericType(KeyType, id<NSCopying >),M13GenericType(KeyType, id<NSCopying>),void * M13__NULLABLE))comparator context:(M13_NULLABLE void >),M13GenericType(KeyType, id<NSCopying>),void * M13__NULLABLE))comparator context:(M13_NULLABLE void

    2.8K20发布于 2018-08-15
  • 来自专栏每日一篇技术文章

    Swift3.0 - 探究Self的用途

    如何实现对象拷贝 第一种方法: 1.遵守协议 NSCopying 2.实现协议 NSCopying 协议 public protocol NSCopying { public func = nil) -> Any } 示例代码: class Student:NSCopying { var name: String required init(name:String){ Student student2.name = "酷走天涯" print(student1.name) print(student2.name) 运行: XUJIE 酷走天涯 分析: 由于NSCopying

    50130发布于 2018-09-14
  • 来自专栏程序员维他命

    面向对象设计的设计模式(六):原型模式

    首先定义学历对象: //================== UniversityInfo.h ================== @interface UniversityInfo : NSObject<NSCopying ]]; [infoCopy setMajor:[_major mutableCopy]]; return infoCopy; } @end 因为学历对象是支持复制的,因此需要遵从<NSCopying ============== Resume.h ================== #import "UniversityInfo.h" @interface Resume : NSObject<NSCopying resumeCopy setUniversityInfo:[_universityInfo copy]]; return resumeCopy; } @end 同样地,简历对象也需要遵从<NSCopying copyWithZone(NSZone zone)方法是接口NSCopying提供的接口。

    75520发布于 2019-03-18
  • 来自专栏nimomeng的自我进阶

    Collection官方文档

    NSDictionary 使用自定义key的几点注意: a) Keys必须实现NSCopying协议。 c) 虽然可以用任何使用了NSCopying协议并继承了hash、isEqual方法的对象,但是最好不要使用large Object来做key,比如NSImage等等,因为这样会损耗性能 如果objects实现了NSCopying协议,则会进行深copy对象;如果没有实现NSCopying协议,会报错。CopyWithZone只会执行一层copy。 ii.

    2.3K40发布于 2018-09-13
  • 来自专栏sktj

    IOS 读写配置文件plist

    7 8 let dic:NSMutableDictionary = NSMutableDictionary() 9 dic.setObject(“Bruce”, forKey:“Name” as NSCopying ) 10 dic.setObject(22, forKey:“Age” as NSCopying) 11 12 let plistPath = Bundle.main.path(forResource

    80520发布于 2019-07-10
  • 来自专栏APP自动化测试

    NSObject头文件解析 / 消息机制 / Runtime解读 (一)

    协议: %@", class_conformsToProtocol([ClassA class], @protocol(NSCopying)) ? (NSCopying))? @"YES": @"NO"); //给对象类添加NSCopying协议 class_addProtocol([aClass class], @protocol(NSCopying )); //检查是否有遵循NSCopying协议 NSLog(@"ClassA confirm to protoco NSCopying: %@", class_conformsToProtocol ([aClass class], @protocol(NSCopying))?

    1.6K31发布于 2019-10-15
  • 来自专栏ShaoYL

    iOS游戏开发之UIDynamic

    可以通过添加边界(boundary),让物理碰撞局限在某个空间中 UICollisionBehavior边界相关的方法 - (void)addBoundaryWithIdentifier:(id <NSCopying >)identifier forPath:(UIBezierPath*)bezierPath; - (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2; - (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier ; - (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier; @property (nonatomic, readonly, copy

    1.2K70发布于 2018-05-11
  • 来自专栏全栈程序员必看

    iOS7 UIKit动力学-碰撞特性UICollisionBehavior 下

    UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying

    51720编辑于 2022-07-08
  • 来自专栏浮躁的喧嚣

    iOS中的Copy

    NSLog(@"%@",p.lastName); log: firstName-AA firstName-AAaa lastNameStr-BB lastNameStr-BB 自定义类的实现copy(NSCopying 协议) 若想令自己写的类具有copy功能,则需要实现NSCopying、NSMutableCopying协议 - (id)copyWithZone:(nullable NSZone *)zone; - mutableCopyWithZone:(nullable NSZone *)zone; 看个例子 #import <Foundation/Foundation.h> @interface Phone : NSObject<NSCopying

    72810编辑于 2023-11-22
  • 来自专栏iOS开发~

    iOS_泛型 Generics __covariant(协变) __contravariant(逆变)

    <NSString *> *array = @[]; 看一下NSArray对泛型的定义: @interface NSArray<__covariant ObjectType> : NSObject <NSCopying NSDictionary对泛型的定义: @interface NSDictionary<__covariant KeyType, __covariant ObjectType> : NSObject <NSCopying instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType <NSCopying

    1.5K10编辑于 2022-07-20
  • 来自专栏滕先生的博客

    UIDynamic 物理引擎概念介绍UIDynamicAnimator(动画者)动力行为(UIDynamicBehavior)一、抽象类 UIDynamicBehavior二、UIGravityBeh

    参数是给这个边界随意取一个标识,碰到边界后会产生一些行为方法,所以要指定一个标识,用于以后引用 (1)设置一个贝塞尔曲线路径为边界 - (void)addBoundaryWithIdentifier:(id <NSCopying addBoundaryWithIdentifier:@"circle" forPath:path]; (2)设置一条线为边界 - (void)addBoundaryWithIdentifier:(id <NSCopying 移除该碰撞所有边界 - (void)removeAllBoundaries; 7.根据边界标识获取路径 - (nullable UIBezierPath *)boundaryWithIdentifier:(id <NSCopying >)identifier; 8.根据边界标识移除边界 - (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier; UICollisionBehaviorDelegate UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying

    3.6K80发布于 2018-05-18
  • 来自专栏正则

    AFNetworking源码探究(十五) —— 请求序列化之架构分析

    @protocol AFURLRequestSerialization <NSObject, NSSecureCoding, NSCopying> @interface AFHTTPRequestSerializer field value to `application/json`. */ @protocol AFURLRequestSerialization <NSObject, NSSecureCoding, NSCopying

    38630发布于 2021-09-03
  • 来自专栏一“技”之长

    谈谈iOS中的原生物理引擎——UIDynamic的应用

    insets: UIEdgeInsets) // 将指令的边界添加到碰撞行为中 open func addBoundary(withIdentifier identifier: any NSCopying , for bezierPath: UIBezierPath) open func addBoundary(withIdentifier identifier: any NSCopying, from p1: CGPoint, to p2: CGPoint) open func boundary(withIdentifier identifier: any NSCopying) -> UIBezierPath : [any NSCopying]? UICollisionBehavior, beganContactFor item: any UIDynamicItem, withBoundaryIdentifier identifier: (any NSCopying

    83010编辑于 2024-06-29
  • 来自专栏浮躁的喧嚣

    设计模式 -- 原型模式

    name: "小白",age: 18) info2.show() //name:小白,age:18 可以看到三份信息需要实例化三次,如果是要n份,就需要实例化n次,这显然不是希望的样子 iOS中,采纳NSCopying 协议,通过这个协议来实现对象的深拷贝 class Info : NSCopying { var name : String var age : Int init

    27810编辑于 2023-11-22
  • 来自专栏大宇笔记

    swift 对象储存地址分析

    = myStruct() var my1 = my my.height = 5 print(my1.height) step4:类对象 class ReferenceObj:NSObject,NSCopying 如果类对象进行地址拷贝和oc一样遵循NSCopying协议,对象执行copy方法即可拷贝地址。

    2K10发布于 2019-01-15
  • 来自专栏大宇笔记

    iOS 程序内存指针和对象的理解

    对copy属性要特别注意:被定义有copy属性的对象必须要 符合NSCopying协议,必须实现- (id)copyWithZone:(NSZone *)zone方法。 (必须实现NSCopying 里copyWithZone) 别用字符串测试,自己写一个类测试。。字符串真是个奇怪的对象,观察不了他的逻辑。 retain strong 都是浅拷贝 copy是深拷贝 #import <Foundation/Foundation.h> @interface copyObj : NSObject<NSCopying

    1.1K20发布于 2019-01-15
领券