1.Difference between frame and bounds
The frame of a rectangle is a view expressed as a location(x,y) and size(width,height)relative to its super view whereas the bounds of a view is expressed as a location(x,y) an size relative to its own co-ordinate system
2.Class hierarchy of UIButton
UIButton->UIControl->UIView->UIResponder->NSObject
3.KVC and KVO
KVC (Key Value Coding)
KVO (Key value Observing) is built on top of KVC.It allows you to observe a KVC path on an object to see when the value changes
4.Objective C
Is a SuperSet of C,Built on top of C.Everything C can do Objective C can do too.But not the other way around.
Methods
So we have an “instance” of a car, now that what do we do with it? Well, we drive it and fill it with petrol, among other things. Driving and filling with petrol apply only to the cars that we use, meaning that when we fill up a car or drive a car, we are only impacting one instance, and not all the cars in the world. Therefore, filling up the car instance is considered an instance method. Itʼs something we do to our instance and only our instance.
The frame of a rectangle is a view expressed as a location(x,y) and size(width,height)relative to its super view whereas the bounds of a view is expressed as a location(x,y) an size relative to its own co-ordinate system
2.Class hierarchy of UIButton
UIButton->UIControl->UIView->UIResponder->NSObject
3.KVC and KVO
KVC (Key Value Coding)
KVO (Key value Observing) is built on top of KVC.It allows you to observe a KVC path on an object to see when the value changes
4.Objective C
Is a SuperSet of C,Built on top of C.Everything C can do Objective C can do too.But not the other way around.
Methods
So we have an “instance” of a car, now that what do we do with it? Well, we drive it and fill it with petrol, among other things. Driving and filling with petrol apply only to the cars that we use, meaning that when we fill up a car or drive a car, we are only impacting one instance, and not all the cars in the world. Therefore, filling up the car instance is considered an instance method. Itʼs something we do to our instance and only our instance.
On the other hand, if we ask the original car class how many colors of car are available, this is a class method because we are no longer only talking about the car we drive around but all cars in general.
5.Difference between a target and a project in XCode
An XCode project is a container that contains all the files and required resources to build one or more software products
A project may contain one or more targets which specify how to build product.
A target defines a single product.Project contain one or more targets each of which produces one product.
6.Difference between categories and Subclassing
categories only add methods, you can't add variables to a class using categories. If the class needs more properties, then it has to be subclassed.(edit: you can use associative storage, I believe).
Categories are a nice way to add functionality while at the same time conforming to an object oriented principle to prefer composition over inheritance.
There are three uses of a category, each of which add methods to a class (note: methods only, not iVars)
Extending an existing Cocoa class
This lets you add your own methods to an existing class. For example, if you want to extend NSString to apply special capitalization, you could create a new class called, say NSString+Capitals. in the NSString+Capitals.h you would have:
@interface NSString (Capitals)
-(NSString *)alternateCaps:(NSString *)aString;
@end
and in NSString+Capitals.m you would implement the method
@implementation NSString (Capitals)
-(NSString *)alternateCaps:(NSString *)aString
{
// Implementation
}
Private methods on a class
This is the same as above, except that the extra methods are declared and defined in the implementation file (.m) Usually a way of having private methods - because they are not in the .h file (which is the one #imported by other classes) they are simply not visible. In this case, the implementation of the methods are done in their own implementation block. e.g
// someClass.m
@interface someClass (extension)
-(void)extend;
@end
@implementation someClass
// all the methods declared in the .h file and any superclass
// overrides in this block
@end
@implementation someClass (extension)
-(void)extend {
// implement private method here;
}
Class Extension (New for 10.5 Leopard)
A simpler way of having private methods. In this special case, the category name is empty and the private methods are implemented in the same block as all the other class methods.
// someClass.m
@interface someClass ()
-(void)extend;
@end
@implementation someClass
// all the methods declared in the .h file and any superclass
// overrides in this block
// Implement private methods in this block as well.
-(void)extend {
// implement private method here;
}
@end
Refer
http://stackoverflow.com/questions/360968/category-usage-in-objective-c?rq=1
You can't add instance variables in categories.
However, you can add storage for your attribute to an object using associative references. Note that if you need to add more than one attribute, rather than adding an associative reference for each, you're probably better off adding a single reference to (say) an NSMutableDictionary, CFMutableDictionaryRef, or NSMapTable and using that for all of your attributes.
Here some Code:
Filename: NSObject+dictionary.h
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (dictionary)
- (NSMutableDictionary*) getDictionary;
@end
Filename: NSObject+dictionary.h
#import "NSObject+dictionary.h"
@implementation NSObject (dictionary)
- (NSMutableDictionary*) getDictionary
{
if (objc_getAssociatedObject(self, @"dictionary")==nil)
{
objc_setAssociatedObject(self,@"dictionary",[[NSMutableDictionary alloc] init],OBJC_ASSOCIATION_RETAIN);
}
return (NSMutableDictionary *)objc_getAssociatedObject(self, @"dictionary");
}
Now every instance (of every class) has a dictionary, where you can store your custom attributes. WithKey-Value Coding you can set a value like this:
[myObject setValue:attributeValue forKeyPath:@"dictionary.attributeName"]
And you can get the value like this:
[myObject valueForKeyPath:@"dictionary.attributeName"]
That even works great with the Interface Builder and User Defined Runtime Attributes.
Key Path Type Value
dictionary.attributeName String(or other Type) attributeValue
No comments:
Post a Comment