iphone Interview Questions part-13

141-Helper Objects

Helper Objects are used throughout Cocoa and CocoaTouch, and usually take the form of a delegate or dataSource. They are commonly used to add functionality to an existing class without having to subclass it.

142-Cluster Class

Class clusters are a design pattern that the Foundation framework makes extensive use of. Class clusters group a number of private concrete subclasses under a public abstract superclass. The grouping of classes in this way simplifies the publicly visible architecture of an object-oriented framework without reducing its functional richness.

143-Differentiate Foundation vs Core Foundation

CoreFoundation is a general-purpose C framework whereas Foundation is a general-purpose Objective-C framework. Both provide collection classes, run loops, etc, and many of the Foundation classes are wrappers around the CF equivalents. CF is mostly open-source , and Foundation is closed-source.

Core Foundation is the C-level API, which provides CFString, CFDictionary and the like.Foundation is Objective-C, which provides NSString, NSDictionary, etc. CoreFoundation is written in C while Foundation is written in Objective-C. Foundation has a lot more classes CoreFoundation is the common base of Foundation and Carbon.

144-Difference between coreData and Database

Database Core Data
Primary function is storing and fetching data Primary function is graph management (although reading and writing to disk is an important supporting feature)
Operates on data stored on disk (or minimally and incrementally loaded) Operates on objects stored in memory (although they can be lazily loaded from disk)
Stores “dumb” data Works with fully-fledged objects that self-manage a lot of their behavior and can be subclassed and customized for further behaviors
Can be transactional, thread-safe, multi-user Non-transactional, single threaded, single user (unless you create an entire abstraction around Core Data which provides these things)
Can drop tables and edit data without loading into memory Only operates in memory
Perpetually saved to disk (and often crash resilient) Requires a save process
Can be slow to create millions of new rows Can create millions of new objects in-memory very quickly (although saving these objects will be slow)
Offers data constraints like “unique” keys Leaves data constraints to the business logic side of the program

 

CoreData Methods and key value

Name Use Key methods
NSManagedObject
  • Access attributes
  • A “row” of data
  • -entity
  • -valueForKey:
  • -setValue:forKey:
NSManagedObjectContext
  • Actions
  • Changes
  • -executeFetchRequest:error:
  • -save
NSManagedObjectModel
  • Structure
  • Storage
  • -entities
  • -fetchRequestTemplateForName:
  • -setFetchRequestTemplate:forName:
NSFetchRequest
  • Request data
  • -setEntity:
  • -setPredicate:
  • -setFetchBatchSize:
NSPersistentStoreCoordinator
  • Mediator
  • Persisting the data
  • -addPersistentStoreWithType:configuration:URL:options:error:
  • -persistentStoreForURL:
NSPredicate
  • Specify query
  • +predicateWithFormat:
  • -evaluateWithObject:

145- Core data vs sqlite.

Core data is an object graph management framework. It manages a potentially very large graph of object instances, allowing an app to work with a graph that would not entirely fit into memory by faulting objects in and out of memory as necessary. Core Data also manages constraints on properties and relationships and maintains reference integrity (e.g. keeping forward and backwards links consistent when objects are added/removed to/from a relationship). Core Data is thus an ideal framework for building the “model” component of an MVC architecture.

To implement its graph management, Core Data happens to use sqlite as a disk store. Itcould have been implemented using a different relational database or even a non-relational database such as CouchDB. As others have pointed out, Core Data can also use XML or a binary format or a user-written atomic format as a backend (though these options require that the entire object graph fit into memory).

146-Retain cycle or Retain loop.

When object A retains object B, and object B retains A. Then Retain cycle happens. To overcome this use “close” method.

Objective-C’s garbage collector (when enabled) can also delete retain-loop groups but this is not relevant on the iPhone, where Objective-C garbage collection is not supported.

147-What is unnamed category.

A named category — @interface Foo(FooCategory) — is generally used to:

i. Extend an existing class by adding functionality.

ii. Declare a set of methods that might or might not be implemented by a delegate.

Unnamed Categories has fallen out of favor now that @protocol has been extended to support @optional methods.

A class extension — @interface Foo() — is designed to allow you to declare additional private API — SPI or System Programming Interface — that is used to implement the class innards. This typically appears at the top of the .m file. Any methods / properties declared in the class extension must be implemented in the @implementation, just like the methods/properties found in the public @interface.

Class extensions can also be used to redeclare a publicly readonly @property as readwrite prior to @synthesize’ing the accessors.

Example:

Foo.h

@interface Foo:NSObject

@property(readonly, copy) NSString *bar;

-(void) publicSaucing;

@end

Foo.m

@interface Foo()

@property(readwrite, copy) NSString *bar;

– (void) superSecretInternalSaucing;

@end

@implementation Foo

@synthesize bar;

…. must implement the two methods or compiler will warn ….

@end

148-Copy vs mutableCopy.

copy always creates an immutable copy.

mutableCopy always creates a mutable copy.

149- Strong vs Weak

The strong and weak are new ARC types replacing retain and assign respectively.

Delegates and outlets should be weak.

A strong reference is a reference to an object that stops it from being deallocated. In other words it creates a owner relationship.

A weak reference is a reference to an object that does not stop it from being deallocated. In other words, it does not create an owner relationship.

150-__strong, __weak, __unsafe_unretained, __autoreleasing.

Generally speaking, these extra qualifiers don’t need to be used very often. You might first encounter these qualifiers and others when using the migration tool. For new projects however, you generally you won’t need them and will mostly use strong/weak with your declared properties.

__strong – is the default so you don’t need to type it. This means any object created using alloc/init is retained for the lifetime of its current scope. The “current scope” usually means the braces in which the variable is declared

__weak – means the object can be destroyed at anytime. This is only useful if the object is somehow strongly referenced somewhere else. When destroyed, a variable with __weak is set to nil.

__unsafe_unretained – is just like __weak but the pointer is not set to nil when the object is deallocated. Instead the pointer is left dangling.

__autoreleasing, not to be confused with calling autorelease on an object before returning it from a method, this is used for passing objects by reference, for example when passing NSError objects by reference such as [myObject performOperationWithError:&tmp];