Archive

Archive for the ‘Xcode’ Category

couldn’t determine absolut path of file…

Got this error message when a directory was deleted on the svn server. A new checkout solved the problem.

Categories: SCM, Xcode

Installing Git

Download latest stable Git release from here and install it.

You can get a version via Git with:

git clone git://git.kernel.org/pub/scm/git/git.git

Categories: Mac OS X, Xcode

Relationship does not have an inverse

Apple wants you to have inverse relationships. See Core Data documentation:

“It is possible for relationships to be navigable in only one direction (if you are never interested in finding out from a department object what employees are associated with it, then you do not have to model that relationship), however you are strongly encouraged always to model relationships in both directions.”

But perhaps yout don’t want/need an inverse relationship between two entities. To disable the XCode model compiler warnings simply check the box to ‘Suppress momc warnings on missing inverse relationships’.

Categories: Xcode

Format sourcecode

Edit->Format->Re-Indent

You can set a shortcut for this:

xcode shortcuts

Categories: Xcode

Change the company name in template files

Paste this into the Terminal application:

defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Super Developers Inc.";}'

Then restart Xcode.

Categories: iOS, Xcode

Turn off the “You are about to undo past the last point this file was saved.” warning

August 31, 2010 1 comment

When you attempt to undo after saving, you will get the following prompt:

“You are about to undo past the last point this file was saved. Do you want to do this?”

To get rid of this warning, enter the following into a terminal window:

defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO

Then restart Xcode.

Categories: Xcode

Xcode plug-ins

Installation

Simply move the product (`pluginName.pbplugin`) to `~/Library/Application Support/Developer/Shared/Xcode/Plug-ins` (create the directory if necessary).

Then restart Xcode.

Plugins

  • Xcode auto assistant: When editing an Objective-C file, the completion popup list will be triggered automatically as you type alphanumeric characters. Download the plugin in here.
Categories: Xcode

iPhone Simulator Screenshots

With this tool it’s really easy to take screenshots:
http://www.curioustimes.de/iphonesimulatorcropper/index.html

Categories: iOS, iOS Simulator, Xcode

Core Data

– Add Core Data Framework and the SQlite library “libsqlite3.dylib” to your project.
– Add #import <Cocoa/Cocoa.h> to your prefix header.
– Create managed object model
– Use NSManagedObject as parent class of your entity classes or generate class out of the object model file/new file/…
– Add following to your App Delegate to set up the Managed Objects and the Managed Object Context:


...
@interface AppDelegate : NSObject <UIApplicationDelegate> {
...
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
...
}
@property (nonatomic, retain, readonly) NSManagedObjectModel
   *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext
   *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator
   *persistentStoreCoordinator;
...

@implementation AppDelegate
...
managedObjectModel = [[NSManagedObjectModel alloc] init];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
   initWithManagedObjectModel:managedObjectModel];
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:
   persistentStoreCoordinator];
...

By convention, you can often get a context from a view controller. It’s up to you, though, to follow this pattern.
When you implement a view controller that integrates with Core Data, you can add an NSManagedObjectContext property.
A view controller typically shouldn’t retrieve the context from a global object such as the application delegate. This tends to make the application architecture rigid. Neither should a view controller typically create a context for its own use. This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.
When you create a view controller, you pass it a context. You pass an existing context, or (in a situation where you want the new controller to manage a discrete set of edits) a new context that you create for it. It’s typically the responsibility of the application delegate to create a context to pass to the first view controller that’s displayed.

Create and save a Managed Object:


ManagedObject* managedObject = (ManagedObject*) [NSEntityDescription
   insertNewObjectForEntityForName:@"ManagedObject"
   inManagedObjectContext:managedObjectContext];
NSError* error;
if(![managedObjectContext save:&error]) {
   // Error handling
   NSLog(@"Unresolved Core Data Save error %@, %@",
     error, [error userInfo]);
}

Create the Request:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
  inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

Set the Sort Descriptor:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
  initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

Set the predicate:


(NSPredicate *) predicate = [NSPredicate
   predicateWithFormat:@"(Name == %@)", @"Fred"];
 [request setPredicate:predicate];

Execute the Request:

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext
   executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
}

Finish Up:

[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

Update managed data object with key-value-coding:

[managedObject setValue:@"Tea Time with Moby" forKey:@"title"];
NSError* error;
if(![managedObjectContext save:&error]) {
   // Error handling
}

Getting a value with key-value-coding:

[managedObject valueForKey:@"title"];

Deleting Managed Object:

[managedObjectContext deleteObject:managedObject];
NSError* error;
if(![managedObjectContext save:&error]) {
   // Error handling
}

Request example:

+(NSMutableArray *) searchObjectsInContext: (NSString*)
   entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL)
  sortAscending : (NSManagedObjectContext *) managedObjectContext
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
   inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

// If a predicate was passed, pass it to the query
if(predicate != nil)
{
[request setPredicate:predicate];
}

// If a sort key was passed, use it for sorting.
if(sortKey != nil)
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
   initWithKey:sortKey ascending:sortAscending];
NSArray *sortDescriptors = [[NSArray alloc]
   initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
}

NSError *error;

NSMutableArray *mutableFetchResults = [[[managedObjectContext
   executeFetchRequest:request error:&error] mutableCopy] autorelease];

[request release];

return mutableFetchResults;
}

+(NSMutableArray *) getObjectsFromContext: (NSString*) entityName : (NSString*)
   sortKey : (BOOL) sortAscending : (NSManagedObjectContext *)
   managedObjectContext
{
return [self searchObjectsInContext:entityName :nil :sortKey
   :sortAscending :managedObjectContext];
}
Categories: CodeSnippets, iOS, Xcode

Daten speichern/laden

Daten können mit dem iPhone auf unterschiedliche Arten gespeichert/geladen werden:

  • NSUserDefaults (schneller Zugriff auf einfache Datentypen)
  • Dateien speichern/laden
  • direktes Aufrufen von SQLite Befehlen
  • verwenden von SQLite Wrapper wie sqlitepersistentobjects
  • Apples Core Data Framework (Persistenz-Framework ähnlich Hibernate)
Categories: CodeSnippets, iOS, Xcode