cocoa-design-patterns

custom Location Manager class using CLLocationManager

谁说我不能喝 提交于 2019-12-22 10:45:50
问题 I'm pretty new to iOS development (my first app) and I faced this issue. I have an iPhone app that should get user's current location in multiple ViewControllers upon user button touch. To prevent redundant code (implementing locationManager:didFailWithError , locationManager:didUpdateToLocation:fromLocation , etc. multiple times in different view controllers) I decided to create a custom class called LocationManager : LocationManager.h @interface LocationManager : NSObject

One UITableView - Multiple DataSource, best design pattern?

僤鯓⒐⒋嵵緔 提交于 2019-12-20 11:33:26
问题 This seems like a typical problem, but I have a UITableView that has identical behavior for two separate data sources. What is the best way of going about designing the class hierarchy to have as little duplication and if/else conditions? The view controller is going to do the same exact thing to both data sources, they're just unique in their data set. Should I have the parent controller just set its data source/respective title? The same issue is relevant as well when using a

How do I serialize a simple object in iPhone sdk?

心不动则不痛 提交于 2019-12-20 08:27:22
问题 I have a dictionary of objects; they are all POCO objects that should be serializable. What technique should I look at for writing these to disk. I'm looking for the simplest option to write a few lists to save state. I think I have 3 options. plist files. However this seems to be limited to only storing predefined objects (strings, numbers etc) not objects (like a person with a name and age). CoreData. (New in 3.0) This would work well; however my data model would need to change to make this

Why do Cocoa-Touch class ivars have leading underscore character?

痞子三分冷 提交于 2019-12-20 02:54:47
问题 Is there some purpose for this convention? 回答1: Apple likes to use underscores to mean "private", according to the Coding Guidelines for Cocoa: Avoid the use of the underscore character as a prefix meaning private, especially in methods. Apple reserves the use of this convention. Use by third parties could result in name-space collisions; they might unwittingly override an existing private method with one of their own, with disastrous consequences. Method names beginning with underscores are

Getting an NSArray of a single attribute from an NSArray

可紊 提交于 2019-12-18 15:34:37
问题 I am facing a very regular scenario. I have an NSArray which has object of a custom type, say Person. The Person class has the attributes: firstName, lastName and age. How can I get an NSArray containing only one attribute from the NSArray having Person objects? Something like: NSArray *people; NSArray *firstNames = [people getArrayOfAttribute:@"firstName" andType:Person.Class] I have a solution of writing a for loop and fill in the firstNames array but I don't want to do that. 回答1: NSArray

Using NSPredicate with Core Data for deep relationships

北城余情 提交于 2019-12-18 11:59:56
问题 I have an NSArrayController, companiesController bound to a top level Core Data entity, Companies . A Company has many Department 's, and a Department has many Employee ; these are represented by the 1-to-many relationships, departments and employees . Based on the attribute salary of an Employee I thought I could dynamically do this for filtering based on salary inside a UI-called method: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY departments.employees.salary < %@",

What exactly is a so called “Class Cluster” in Objective-C?

偶尔善良 提交于 2019-12-17 02:27:06
问题 I was reading that NSArray is just such a thing. Sounds heavy. I have 7 really fat books here on my desk about Objective-C, Cocoa and C. None of them mention Class Cluster at all, at least I can't find it in the Index at the back of the books. So what's that? 回答1: From Apple's docs.... In short it's a design pattern used in the Foundation framework, which is probably why it's not mentioned in ObjC books. A class cluster is an architecture that groups a number of private, concrete subclasses

Why do some objects not need to be initialized before use in objective-c?

≯℡__Kan透↙ 提交于 2019-12-10 15:58:22
问题 Why do some objects not need to be initialized before use in objective-c? For example why is this NSDate *today = [NSDate date]; legal? 回答1: They are initialized within the date method. This is a common way to create autoreleased objects in Objective-C. Allocators of that form are called convenience allocators. To learn more about that, read the "Factory Methods" paragraph in Apple's Cocoa Core Competencies document about Object Creation: http://developer.apple.com/library/mac/#documentation

MVC in a Cocoa document-based application

老子叫甜甜 提交于 2019-12-07 13:01:05
问题 I'm going through a refactoring and reorganization of my application at the moment. I've realized that some of the separation between models and views, and their controllers has diminished and I wish to do some cleaning up. I have several key classes used in my app: NSPersistentDocument, NSWindowController, and a model class. The NSPersistentDocument class acts as a "model-controller"; it owns an instance of the model class, and manages all interactions with the model. The NSWindowController

Coordinating Controller design pattern in Cocoa Touch

痴心易碎 提交于 2019-12-07 07:46:02
问题 I'm creating an iOS application with lots of custom views, so, using default Cocoa views was not an option. Then, I decided to go with the Coordinating / Mediator Controller design patter (learned in Apress - Pro Objective-C Design Patterns for iOS). From the delegate, I create a rootViewController pointing to the view in my coordinating controller: self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; coordinatingController = [C6CoordinatingController sharedInstance]