My goal is to be able to create a unique string based on a timestamp and some other information. I will be doing this many times through-out the code base, so code reuse and maintainability implies that I should make one spot to do this.
我的目标是能够基于时间戳和一些其他信息创建唯一的字符串。我将在代码库中多次执行此操作,因此代码重用和可维护性意味着我应该创建一个这样的位置。
Since this unique ID will always be stored into an NSString, I could make a category on NSString that will create this unique id...
由于这个唯一的ID将永远存储在NSString中,我可以在NSString上创建一个类别来创建这个唯一的ID ...
NSString *uniqueString = [NSString stringWithTimestamp:timestamp andName:name];
Or I could create a custom helper class with a class method to return a unique string string...
或者我可以使用类方法创建一个自定义帮助器类来返回唯一的字符串字符串...
NSString *uniqueString = [HelperClass stringWithTimestamp:timestamp andName:name];
Wouldn't creating the helper class typically be better because I can import it into other projects and use that method, as well as other unrelated methods I may have created (easily contained all in one file)? If I used categories, they'd be spread out in a bunch of different files.
不会创建辅助类通常会更好,因为我可以将它导入到其他项目并使用该方法,以及我可能创建的其他无关方法(很容易包含在一个文件中)?如果我使用类别,它们会分散在一堆不同的文件中。
But this can't be right, because with this logic there would rarely be a need for categories, and just always create a helper class for a project with these helper methods in it...I must be missing something!
但这不可能是正确的,因为有了这个逻辑,很少需要类别,并且总是为一个项目创建一个帮助器类,其中包含这些帮助器方法......我必须遗漏一些东西!
3 个解决方案
#1
1
I will suggest the helper class approach since the unique id method has no relation to NSString class other than the return type. Just imagine, in future if you plan to create an NSInteger as unique id then this method wont have any relation to NSString class. So a helper class is the right approach here.
我将建议帮助器类方法,因为唯一的id方法与返回类型以外的NSString类没有关系。想象一下,将来如果你打算创建一个NSInteger作为唯一id,那么这个方法与NSString类没有任何关系。所以帮助类是正确的方法。
#2
1
For the most part, this is a matter of personal preference: you pick what you think would provide you with better readability, because the performance and maintainability would be the same.
在大多数情况下,这是个人偏好的问题:你选择你认为可以提供更好的可读性,因为性能和可维护性是相同的。
Another choice is to create a category on NSDate
, so you can call it the way you call an instance method (as opposed to calling a class method):
另一种选择是在NSDate上创建一个类别,因此您可以像调用实例方法一样调用它(而不是调用类方法):
NSString *uniqueString = [timestamp uniqueStringWithName:name];
#3
0
I would say helper class is better for multiple helper methods as you say across multiple classes, while a category works well when the class you are extending will be overwritten and you want to preserve your extensions, i.e. in case of NSManagedObject subclasses generated from core data model, or classes you will be updating in a subproject managed with CocoaPods.
我会说帮助类对于多个辅助方法更好,就像你在多个类中说的那样,而当你扩展的类被覆盖并且你想保留你的扩展时,一个类很好用,即在核心数据生成的NSManagedObject子类的情况下您将在使用CocoaPods管理的子项目中更新的模型或类。
#1
1
I will suggest the helper class approach since the unique id method has no relation to NSString class other than the return type. Just imagine, in future if you plan to create an NSInteger as unique id then this method wont have any relation to NSString class. So a helper class is the right approach here.
我将建议帮助器类方法,因为唯一的id方法与返回类型以外的NSString类没有关系。想象一下,将来如果你打算创建一个NSInteger作为唯一id,那么这个方法与NSString类没有任何关系。所以帮助类是正确的方法。
#2
1
For the most part, this is a matter of personal preference: you pick what you think would provide you with better readability, because the performance and maintainability would be the same.
在大多数情况下,这是个人偏好的问题:你选择你认为可以提供更好的可读性,因为性能和可维护性是相同的。
Another choice is to create a category on NSDate
, so you can call it the way you call an instance method (as opposed to calling a class method):
另一种选择是在NSDate上创建一个类别,因此您可以像调用实例方法一样调用它(而不是调用类方法):
NSString *uniqueString = [timestamp uniqueStringWithName:name];
#3
0
I would say helper class is better for multiple helper methods as you say across multiple classes, while a category works well when the class you are extending will be overwritten and you want to preserve your extensions, i.e. in case of NSManagedObject subclasses generated from core data model, or classes you will be updating in a subproject managed with CocoaPods.
我会说帮助类对于多个辅助方法更好,就像你在多个类中说的那样,而当你扩展的类被覆盖并且你想保留你的扩展时,一个类很好用,即在核心数据生成的NSManagedObject子类的情况下您将在使用CocoaPods管理的子项目中更新的模型或类。