objective-c-category

Advice on how to catch “attempt to insert nil object” from a device needed

拜拜、爱过 提交于 2020-01-01 15:11:06
问题 Here is a situation: Hockeyapp and testflight every now and then complain about me "attempting to insert nil object" in mutable dictionaries/arrays. I know the right thing is to check for nil all the time, and I do when it makes sense.. Our testers can not catch those crashes, but AppStore users obviously can. My guess is that sometimes server returns NSNulls when it should not. So not to insert checks for nil everywhere in the huge project my idea was to create a separate target for the

Category for a class that conforms to a protocol [duplicate]

こ雲淡風輕ζ 提交于 2019-12-25 11:54:06
问题 This question already has answers here : Can a category implement a protocol in Objective C? (1 answer) Making class conform to protocol with category for existing methods (2 answers) Closed 6 years ago . I'm trying to implement a category for a UIViewController and I want to be certain that the object conforms to a certain protocol. Something like this: #import <UIKit/UIKit.h> @interface UIViewController<MyProtocol> (Category) @end Is this possible? 回答1: Swap category and protocol:

Category for a class that conforms to a protocol [duplicate]

醉酒当歌 提交于 2019-12-25 11:54:05
问题 This question already has answers here : Can a category implement a protocol in Objective C? (1 answer) Making class conform to protocol with category for existing methods (2 answers) Closed 6 years ago . I'm trying to implement a category for a UIViewController and I want to be certain that the object conforms to a certain protocol. Something like this: #import <UIKit/UIKit.h> @interface UIViewController<MyProtocol> (Category) @end Is this possible? 回答1: Swap category and protocol:

Can Xcode tell me if I forget to include a category implementation in my target?

北慕城南 提交于 2019-12-24 18:19:10
问题 I have a category that I import in a .m thusly: #import "UIView+Additions.h" If I forget to add UIView+Additions.m to my target, I won't know until runtime when the runtime can't find my selector. Is there a way to find out at compile time (or probably link time) that I forgot to include a category's implemtation? 回答1: This macro works! #ifndef HJBObjCCategory_h #define HJBObjCCategory_h #define HJBObjCCategoryInterface( c , n ) \ \ extern int c##n##Canary; \ \ __attribute__((constructor))

Automatically adjusting height of a UILabel

狂风中的少年 提交于 2019-12-23 19:33:09
问题 I'm using the following two methods (one is a category of NSString and the other a category of UILabel ) to automatically adjust the height of a label based on the text inside it. It's working fine for the mostpart, but it's having some unpredictable results. I'm not quite sure where the issue may be occuring and I'm hoping some of you fine folks can offer a helping hand. First off, here are the methods in question: NSString Category: - (CGFloat) fontSizeWithFont: (UIFont *) font

How a class extension works as a means of implementing private methods

不想你离开。 提交于 2019-12-22 18:47:13
问题 I believe a popular way to declare "private methods" in Objective-C is to create its class extension and declare methods that you would like to make as private. I would like to know more in detail on how an class extension makes the methods work as private. Update: I asked this question with the term empty category which is incorrect. I now changed it as class extension 回答1: That's not an "empty category", it's a class extension. Read Bbum's explanation of them at the link I provided. 回答2:

How can I declare a private property within a named category?

故事扮演 提交于 2019-12-20 12:28:35
问题 I know about the possibility of declaring private properties on a class by putting them inside an unnamed category on that class declared in the implementation ( .m ) file of that class. That's not what I want to do. I'm dealing with a named category on a class that adds some functionality to that class. For this functionality, it would help me very much to have a private property to use in my category - so the usual way of achieving this (described above) doesn't seem to work for me. Or does

Objective-C class names with a +

巧了我就是萌 提交于 2019-12-19 19:43:03
问题 Does a class name, say UIImage+Something or UIImageView+Somethingelse , mean that it acts like a custom UIImage or UIImageView ? 回答1: I think you are looking at the file names of Categories, not Classes. The plus character + is not allowed in class names or any other identifier in Objective-C. An Objective-C category is a way of adding methods (but not instance variables) to a class you don't necessarily have the source to. For example, if you frequently want to make upside-down copies of

What's wrong with using a category on NSObject to provide a default protocol implementation?

人盡茶涼 提交于 2019-12-19 17:42:13
问题 I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling performSelector: is out (and NSInvocation has problems cooperating with ARC) 3: This solution, IMO, pretends to be universal but has all the drawbacks of 1 I eventually came up with this idea: @protocol MyProtocol <NSObject> @optional -(void

What's wrong with using a category on NSObject to provide a default protocol implementation?

非 Y 不嫁゛ 提交于 2019-12-19 17:42:02
问题 I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling performSelector: is out (and NSInvocation has problems cooperating with ARC) 3: This solution, IMO, pretends to be universal but has all the drawbacks of 1 I eventually came up with this idea: @protocol MyProtocol <NSObject> @optional -(void