KVC and KVO

What is KVC and KVO? Give an example of using KVC to set a value.
KVC stands for Key-Value Coding. It's a mechanism by which an object's properties can be accessed using string's at runtime rather than having to statically know the property names at development time. KVO stands for Key-Value Observing and allows a controller or class to observe changes to a property value.
Let's say there is a property name on a class:
@property (nonatomic, copy) NSString *name;
We can access it using KVC:
NSString *n = [object valueForKey:@"name"]
And we can modify it's value by sending it the message:
[object setValue:@"Mary" forKey:@"name"]

Question 12

What are blocks and how are they used?
Blocks are a way of defining a single task or unit of behavior without having to write an entire Objective-C class. Under the covers Blocks are still Objective C objects. They are a language level feature that allow programming techniques like lambdas and closures to be supported in Objective-C. Creating a block is done using the ^ { } syntax:
 myBlock = ^{
    NSLog(@"This is a block");
 }
It can be invoked like so:
myBlock();
It is essentially a function pointer which also has a signature that can be used to enforce type safety at compile and runtime. For example you can pass a block with a specific signature to a method like so:
- (void)callMyBlock:(void (^)(void))callbackBlock;
If you wanted the block to be given some data you can change the signature to include them:
- (void)callMyBlock:(void (^)(double, double))block {
    ...
    block(3.0, 2.0);
}

Comments

  1. When beginning longboarding, one question quickly arises: Do I really need to get skate shoes? The answer to that question depends a lot on the individual. No one needs a purpose-built shoe to skate on the driveway or cruise down the sidewalk. But the best longboard shoe is in its element once serious speeds and longer rides come into the equation. The flat surface of the deck dictates that the outsoles also be flat, and the unique forces and friction of longboarding require materials that can withstand abuse. The following shoes break many molds, and in the process solve the riddle of longevity and board feel in their own ways longboardzi.com.

    ReplyDelete
  2. STAD Solution is an IT Training and Development company. We provide Best Software testing Training in Ahmedabad.

    Thank you for sharing this website.
    Also visit STAD Solution

    ReplyDelete

Post a Comment

Popular posts from this blog

iOS Architecture

Property vs Instance Variable (iVar) in Objective-c [Small Concept but Great Understanding..]

setNeedsLayout vs layoutIfNeeded Explained