将同一类别添加到多个类 [英] Adding the same category to multiple classes

查看:97
本文介绍了将同一类别添加到多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Objective-C类别,我希望添加到多个类,而不重复类别中包含的代码。



我已经在上有类别NSManagedObject 子类( Book



一种方法是将类别添加到其通用的超类( NSManagedObject ),但是当我想将方法​​添加到三个NSManagedObject子类()时,会有类别的方法添加到所有 NSManagedObject )。

另一个解决方案是子类 NSManagedObject ,然后有 Book 继承 NSManagedObject 子类。这是最清洁,最直接的方法。这种方法的最大缺点是当数据模型发生变化并且Xcode重新生成子类时,它会将它们重置为继承自 NSManagedObject 而不是 SubclassedManagedObject 。如果可能,我想避免使用mogenerator / Xmo'd。



是否可以在多个类上添加单个类别,而不重复代码?



感谢。

解决方案

我还不知道一个干净的方法这在Objective-C,但与Swift 2.0这可以使用协议扩展通过添加功能和/或属性到现有协议实现。

 协议编号{
func number() - > Int
}

扩展编号{
func number() - > Int {
return Int(arc4random())%10
}
}

类书:编号{

}

class Chapter:编号{

}

类页面:编号{

}

let myBook = Book()
let myChapter = Chapter()
let myPage = Page()

print(myBook.number()= \(myBook.number )
print(myPage.number()= \(myPage.number()))
print(myChapter.number() )

正确实现了 number()三个类( Book Chapter Page ): / p>

  myBook.number()= 5 
myChapter.number()= 2
myPage.number = 8


I have an Objective-C category that I'd like to add to multiple classes without duplicating the code contained in the category. I simply want to add the same methods to multiple classes.

I have existing categories on NSManagedObject subclasses (Book, Chapter, Page) and I would like to add common functionality throughout these subclasses in a clean and maintainable way.

One way would be to add the category to their common superclass (NSManagedObject), but that has the consequence of adding the category's methods to all NSManagedObject subclasses when I want to add the methods to three NSManagedObject subclasses (Book, Chapter, Page).

Another solution would be to subclass NSManagedObject and then have Book, Chapter, and Page inherit from that NSManagedObject subclass. This is the cleanest, most straight forward approach. The big downside with this approach is when the data model changes and Xcode regenerates the subclasses, it will reset them back to inheriting from NSManagedObject instead of SubclassedManagedObject. I'd like to avoid using something like mogenerator/Xmo'd if possible.

Is it possible to add a single category on multiple classes without duplicating code?

Thanks.

解决方案

I'm still unaware of a clean way to do this in Objective-C, but with Swift 2.0 this can be implemented using Protocol Extensions by adding functions and/or properties to an existing protocol. The protocol can then be adopted by an arbitrary number of classes, structs, and/or enums.

protocol Numbered {
    func number() -> Int
}

extension Numbered {
    func number() -> Int {
        return Int(arc4random()) % 10
    }
}

class Book : Numbered {

}

class Chapter : Numbered {

}

class Page : Numbered {

}

let myBook = Book()
let myChapter = Chapter()
let myPage = Page()

print("myBook.number() = \(myBook.number())")
print("myChapter.number() = \(myChapter.number())")
print("myPage.number() = \(myPage.number())")

correctly implements number() on all three classes (Book, Chapter, Page):

myBook.number() = 5
myChapter.number() = 2
myPage.number() = 8

这篇关于将同一类别添加到多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆