objective-c-category

How to subclass NSTextAttachment?

跟風遠走 提交于 2019-11-28 17:39:19
Here is my problem: I use Core Data to store rich text input from iOS and/or OS X apps and would like images pasted into the NSTextView or UITextView to: a) retain their original resolution, and b) on display to be scaled to fit the textView correctly, which means scaling based on the size of the view on the device. Currently I am using - (void)textStorage:(NSTextStorage *)textStorage didProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta to look for attachments and to then generate an image with a scale factor and assigning it to the

When to use categories and when to use subclassing? [closed]

帅比萌擦擦* 提交于 2019-11-28 17:30:26
Can anybody tell me when to use categories and when to use subclassing in Objective-C? Also please tell me the advantages and disadvantages of them. An objective-c category is useful if you want to alter the behavior of ALL instances of the class, with minimal code. Subclassing is more useful if you want to alter the behavior of only certain instances, and retain the original method for others. Categories can be dangerous, especially if you cannot view the source of the original method, so you should generally use subclasses on third-party and private frameworks rather than a category.

Protocol versus Category

时光总嘲笑我的痴心妄想 提交于 2019-11-28 15:39:57
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you use one over the other? mipadi A protocol is the same thing as an interface in Java: it's essentially a contract that says, "Any class that implements this protocol will also implement these methods." A category, on the other hand, just binds methods to a class. For example, in Cocoa , I can create a category for NSObject that will allow me to add methods to the NSObject class (and, of course, all subclasses), even though I don't really have access to NSObject . To summarize: a protocol specifies

NSManagedObject subclass property in category

怎甘沉沦 提交于 2019-11-28 02:22:01
The NSManagedObject subclass property are created in its category file, which is very wired since category can only have method. The details are as below: (1).I have created an entity called BibleAudio in .xcdatamodeld file with several attributes as below. (2). the xcode generated objective c files are "BibleAudio + CoreDataProperties.h", "BibleAudio + CoreDataProperties.m" and "BibleAudio.h", "BibleAudio.m" as following: (3). within "BibleAudio + CoreDataProperties.h", BibleAudio's attributes are declared as property here (see following); while in "BibleAudio.h", it is empty. As far as I

iOS 5: Make NSString Category include NSCFConstantString?

醉酒当歌 提交于 2019-11-28 00:07:01
问题 I have an NSString category class ( NSString+URLEncoding.h ). I am running into and unknown selector crash, because the string I am calling the category method has been optimized into an NSCFConstantString by iOS. -[__NSCFConstantString URLEncodedString]: unrecognized selector sent to instance 0x290174 I learned of the NSCFConstantString vs. NSCFString optimizations in iOS 5 from: http://www.cocoanetics.com/2012/03/beware-of-nsstring-optimizations/ Is anyone aware of how I can get the

Add rounded corners to all UIImageViews

﹥>﹥吖頭↗ 提交于 2019-11-27 17:01:06
I would like to add some rounded corners to all of the UIImageViews in my project. I have already got the code working, but am having to apply it to every image; should I subclass UIImageView to add this? If so, can someone give me some pointers as to how to do this? Here is the code - (void)viewDidLoad { [super viewDidLoad]; NSString *mainpath = [[NSBundle mainBundle] bundlePath]; welcomeImageView.image = [UIImage imageWithContentsOfFile:[mainpath stringByAppendingString:@"/test.png"]]; welcomeImageView.layer.cornerRadius = 9.0; welcomeImageView.layer.masksToBounds = YES; welcomeImageView

Why can't categories have instance variables?

家住魔仙堡 提交于 2019-11-27 15:49:14
问题 I understand we can use associative references to invoke ivar-like behavior in categories. But what's the specific reason behind not being able to declare new ivars in categories? Is it because we would invade the private space of the class? Or is there any other reason? If yes, I would appreciate an example that shows the ability to declare ivars in categories breaking whatever it breaks. 回答1: Think of the Objective-C's ivars like a plain old C-structure. When you instantiate an instance of

Creating a category for classes that implement a specific protocol in Objective-C?

夙愿已清 提交于 2019-11-27 15:12:58
问题 Short problem description Can I extend UIView with a category, but have it only work on subclasses that implement a specific protocol ( WritableView )? I.e. can I do something like the following? @interface UIView<WritableView> (foo) // SYNTAX ERROR - (void)setTextToDomainOfUrl:(NSString *)text; - (void)setTextToIntegerValue:(NSInteger)value; - (void)setCapitalizedText:(NSString *)text; @end @implementation UIView<WritableView> (foo) // implementation of 3 above methods would go here @end

How to subclass NSTextAttachment?

半世苍凉 提交于 2019-11-27 10:38:07
问题 Here is my problem: I use Core Data to store rich text input from iOS and/or OS X apps and would like images pasted into the NSTextView or UITextView to: a) retain their original resolution, and b) on display to be scaled to fit the textView correctly, which means scaling based on the size of the view on the device. Currently I am using - (void)textStorage:(NSTextStorage *)textStorage didProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger

Class extension vs class category

戏子无情 提交于 2019-11-27 10:14:28
Class extensions @interface Class () are a lot more powerful and can inject variables into the class. Categories @interface Class (Category) can't. What other differences are there, and when should one use a category over a class extension? The main difference is that with an extension, the compiler will expect you to implement the methods within your main @implementation , whereas with a category you have a separate @implementation block. So you should pretty much only use an extension at the top of your main .m file (the only place you should care about ivars, incidentally) -- it's meant to