objective-c-category

Properties in Categories

筅森魡賤 提交于 2019-12-11 19:52:46
问题 Why is it allowed to declare properties in categories when neither they nor their accessor methods are synthesized? Is there any performance overhead involved? Is categorisation purely a compiler technique? I'm trying to understand how categories work. This just explains what to do and what not to do. Are there any sources which go into more detail? EDIT : I know that I can use associated references. Thats not what I'm asking for. I want to know why are the properties not synthesised? Is

Using a UIColor category in iOS 7 and Objective c

依然范特西╮ 提交于 2019-12-11 08:29:15
问题 I have a UIColor+MyLayout.m file such as: @implementation UIColor (Layout) - (UIColor *) textBackground { UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f]; return lightGreen; } @end I have added the .h file to my viewcontroller.m, but how do I call this into a UIColor? UIColor *myColor = ? 回答1: Would be better if you do the following: @implementation UIColor (Layout) + (UIColor *) textBackground { UIColor *lightGreen = [UIColor

Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'

只愿长相守 提交于 2019-12-11 03:14:14
问题 I have an NSDate category with following method @implementation NSDate (DateUtility) +(NSString *)dateTimeStringForDB { NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init]; [dateFormatForDB setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *aDateStr= [dateFormatForDB stringFromDate:self]; [dateFormatForDB release]; return aDateStr; } @end with this definition I receive a warning . Incompatible pointer type sending 'Class' to parameter of type 'NSDate *' However type casting self

If this is the right way to use a customised string property in Objective C, why can’t I extract the correct numeric value?

时光毁灭记忆、已成空白 提交于 2019-12-10 22:57:16
问题 I am revising an early project where I use tags to identify 1-of-5, 1-of-16 or 1-of-10 UIButtons . I want to replace the tags with a customised property based on my understanding of this answer. The property called myInfo consists of a string followed by an integer. This may well be a tag by another name but it makes a message source uniquely identifiable in a way that a simple integer tag does not, clearing magic numbers from my code and, hopefully, improving the documentation. The property

Implicit conversion of 'BOOL' (aka 'signed char') to 'id' ,objc_setAssociatedObject

戏子无情 提交于 2019-12-10 17:16:16
问题 I am using an associated reference as storage for a property of my category header file contains : @interface UIImageView (Spinning) @property (nonatomic, assign) BOOL animating; @end implementation is - (void)setAnimating:(BOOL)value { objc_setAssociatedObject(self, animatingKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } However, I am getting the warning for the line above Implicit conversion of 'BOOL' (aka 'signed char') to 'id' is disallowed with ARC if you know what I am doing wrong

Objective-C category is not loaded - How to debug this

冷暖自知 提交于 2019-12-10 13:17:04
问题 I have two projects which the RestKit framework. One project works without problems but another project fails, as soon as the RestKit framework is used. I found out that the failing code is this: return [anNSString MD5]; The MD5 method is a category method and is imported like this: #import "NSString+MD5.h" However, in one project, I keep getting the following error: -[__NSCFString MD5]: unrecognized selector sent to instance 0x88a3390 I understand the basics of categories, and that they can

How to Use Objective-C Categories

浪子不回头ぞ 提交于 2019-12-09 15:46:13
问题 When you implement a category of a class in a file, will all the instances of that class be of the category by default? I'm new to Objective-C and I'm trying to make my uneditable UITextView non-selectable. I came across this answer using a category: https://stackoverflow.com/a/8013538/1533240 Which has the following solution: @implementation UITextView (DisableCopyPaste) -(BOOL) canBecomeFirstResponder { return NO; } @end I added the snippet to my code, but it doesn't seem to be working in

How do I define a category that adds methods to classes which implement a particular protocol?

蓝咒 提交于 2019-12-08 02:49:57
问题 I want to add some methods to subclasses of NSManagedObject that implement the SOManagedObject protocol. I've tried defining it like this: @interface NSManagedObject <SOManagedObject> (MyExtensionMethods) ... @end ...but that doesn't seem to be valid. How do I define a category that adds methods to classes which implement a particular protocol? 回答1: Defining such a category on all such classes in general is not easily solvable. But your actual problem seems simpler: How does one add a

How to access user input from UIAlertView completion block without delegation?

房东的猫 提交于 2019-12-06 13:31:59
问题 Using iOS6: I would like to retrieve the text entered by a user into a UITextField associated with the UIAlertView. I am aware that I could achieve the desired result with a delegate however I am curious about solving this issue with a callback function as I believe this may be an interesting pattern. I began by examining a common pattern for category extension of the UIAlertView class. Code below. Thanks in advance for any suggestions. import <UIKit/UIKit.h> @interface UIAlertView (Block) -

How do I define a category that adds methods to classes which implement a particular protocol?

六月ゝ 毕业季﹏ 提交于 2019-12-06 13:09:58
I want to add some methods to subclasses of NSManagedObject that implement the SOManagedObject protocol. I've tried defining it like this: @interface NSManagedObject <SOManagedObject> (MyExtensionMethods) ... @end ...but that doesn't seem to be valid. How do I define a category that adds methods to classes which implement a particular protocol? Defining such a category on all such classes in general is not easily solvable. But your actual problem seems simpler: How does one add a category method to NSManagedObject for use only with subclasses that implement <SOManagedObject> ? This is solvable