G8Production

Su di me

G8Production sito ufficiale di Daniele Galiotto. Freelancer Apple Developer.
Chi sono, programmazione, sport, vita, viaggi, iPhone, Mac, Objective-C, iOS, C#, birra e...

Seguendo

http://store.apple.com/us is offline. 

We’ll be back. :)

applestore

NSError details: How to display a good NSError

NSError details: How to display a good NSError - www.g8production.comWhen you get a NSError object back from a method like this 

  [myContext save:&error]; 

you can read a detailed explanation of the error using this method (of class):

+(void)getErrors:(NSError *)error
{
  if (!error)
    return;

  NSLog(@”Failed to %@”, [error localizedDescription]);
  NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
  if(detailedErrors && [detailedErrors count] > 0) 
    {
      for(NSError* detailedError in detailedErrors) 
        {
          NSLog(@”DetailedError: %@”, [detailedError userInfo]);
        }
    }
  else
    {
       NSLog(@”%@”, [error userInfo]);
    }
}

Write the getErrors method and update your code in this way:

NSError *error = nil;
[myContext save:&error];
if(error != nil)
  {
    [myClass getErrors:error];  //it’s a method of class
  } 
else
  {
    NSLog(@”The best movie of all time, is Back To The Future!”);
  }

Good Luck :)

apple ios objective-c nserror

Core Data: get the real type of NSNumber entity properties

When you create a entity in Core Data, you can choose between some data types for the properties (String, Float, Boolean, Integer 32, Integer 64, etc…).

Core Data: get the real type of NSNumber entity propertiesFinally, when you create the NSManagedObject subclass of the entity (you are using the Data Model), the property data types are modeled to NSObject classes.

String properties are modeled to NSString properties; 

Int properties are modeled to NSNumber properties;
Float properties are modeled to NSNumber properties; 
Boolean properties are modeled to NSNumber properties; 

So int 0 is modeled to 0; float 0 is modeled to 0.0; boolean false is modeled to 0.

Yes ok, now i can obtain the correct value of a property using

[myProperty intValue]; //0
[myProperty floatValue];  //0.0
[myProperty boolValue];  //false

But, i don’t know the data type of the “myProperty” property so i want to determine it! Core Data and Objective-C will help me!

First: get attributes for the entity. This method will returns a NSDictionary where the keys are the attribute names (they are the property names) and the values are instances of NSAttributeDescription.

NSDictionary *entityAttributes = [[currentEntity entity] attributesByName];


Second: get the property data types and determine the real type. This method will return a NSAttributeType.

NSString *currentPropertyName = @”isBooleanProperty”;

if([myProperty isKindOfClass:[NSNumber class]])
{

NSAttributeDescription *propertyAttributeDescription = [entityAttributes valueForKey:currentPropertyName];

//if bool value
if([finalValueAttributeDescription attributeType] == NSBooleanAttributeType)

{   //write here your code! } 

Valid values for NSAttributeType (it’s a enum) are listed below:

typedef enum {
NSUndefinedAttributeType = 0,
NSInteger16AttributeType = 100,
NSInteger32AttributeType = 200,
NSInteger64AttributeType = 300,
NSDecimalAttributeType = 400,
NSDoubleAttributeType = 500,
NSFloatAttributeType = 600,
NSStringAttributeType = 700,
NSBooleanAttributeType = 800,
NSDateAttributeType = 900,
NSBinaryDataAttributeType = 1000,
NSTransformableAttributeType = 1800,
NSObjectIDAttributeType = 2000
} NSAttributeType;

Good Luck :)

NSAttributeType apple core data core data iOS objective-c NSNumber NSBooleanAttributeType

How to force rotation of a UIView with CGAffineTransform

How to force rotation of a UIView with CGAffineTransformSimply write this code in the UIView Controller:

int degrees = 90; //change me

[self.view setTransform:CGAffineTransformMakeRotation(M_PI * (degrees) / 180)];


It will rotate the UIView of 90°.

The M_PI means pi, 3.141592654, so M_PI * (90) / 180 will rotate the UIView of 1.57… radians (90 degrees)

Good Luck :)

2 note apple objective-c CGAffineTransformMakeRotation ios xcode

Core Data: where clause with NSDate and other arguments

Core Data: where clause with NSDate and other arguments - www.g8production.comWith the Core Data framework you can manage the model objects in the your database.

With the NSFetchRequest class you can describe search criteria used to retrieve datas from a persistent store.

Finally, with the NSPredicate class you can define logical conditions to filter a search.

Well, when you want to create a new NSPredicate, you can use two important methods:

+ (NSPredicate *)predicateWithFormat:(NSString *)format, …

or

+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(NSArray *)arguments


The first method is used for easy query into the model, contrariwise the second method is used for complex query with one or more arguments. 

So, how can i configure a NSPredicate to compare datas and/or other arguments?

Follow the code below, i will explain how to create a new NSPredicate with and without arguments:

//simple predicate format, without arguments
NSString *simplePredicateFormat = [NSString stringWithFormat:@”isDeleted == FALSE”];
 

//complex predicate format, with arguments
NSDate *currentDate = [NSDate date];

NSString *complexPredicateFormat = [NSString stringWithFormat:@”isDeleted == FALSE AND (activationDate == nil OR activationDate <= %@)”];

NSArray *withArguments = [NSArray arrayWithObjects:currentDate, nil];
 

NSFetchRequest *simpleFetchRequest = [[NSFetchRequest alloc] init];

NSFetchRequest *complexFetchRequest = [[NSFetchRequest alloc] init];
 

NSPredicate *simplePredicate = [NSPredicate predicateWithFormat:simplePredicateFormat];

NSPredicate *complexPredicate = [NSPredicate predicateWithFormat:complexPredicateFormat argumentArray:withArguments];

[simpleFetchRequest setPredicate:simplePredicate];

[complexFetchRequest setPredicate:complexPredicate];

…execute your query…

Good Luck :)

core data nspredicate objective-c ios apple

Core Data: How to propagate changes to relationships, using delete rules

The Core Data framework provides generalized and automated solutions to common tasks associated with objects in your SQLite Database.

In Core Data you can set up a delete rule for delete any relationship in the current entity.
Many don’t know that you can use a delete rule for propagate changes to a relationship.

Now i will explain you how to do it in XCode.

Step one - create and fill the data model:

  • Add a Data Model to the solution (right click to the ProjectName/New File/Core Data/Data Model);
  • Create entities and relationships (i will use this entities, A, A1, B, C and D, like in the picture).

Data Model - www.g8production.com

  • Select the relations (you are using them for propagate changes), in the Utility Panel on the right, click on the Show Data Model Inspector button and after change the Delete Rule to Cascade (i will change the delete rule to cascade only in the relationB, relationC and relationD relationships).
Cascade - ww.g8production.com

Step two - create NSManagedObject subclasses

  • Select all entities in the Data Model 
  • Add the NSManagedObject subclasses to the project (right click to the ProjectName/New File/Core Data/NSManagedObject subclasses);

Step three - use your Data Model :)

Step four - propagate changes to relationships

  • Simply use this code with the current entity
-(void)propagateToCascade:(id)entity
{
//set delete property to "true"
[entity setValue:[NSNumber numberWithBool:TRUE] forKey:@"delete"];

//NSEntityDescription for entity
NSEntityDescription *entityDescription = [entity entity];

 //get relationship
 NSDictionary *relationsDictionary = [entityDescription relationshipsByName];

 //for each relation
 for (NSString *relationKey in [relationsDictionary allKeys]) 
 {
 //get relation descriptor
 NSRelationshipDescription *relationDescription = [relationsDictionary objectForKey:relationKey];

 //if current relation has not the "cascade" deletion rule...
 if([relationDescription deleteRule] != NSCascadeDeleteRule)
 continue;

 //...else...

 //if is a multi-entities-relation (is a NSSet)
 if([relationDescription isToMany])
 {
 NSMutableSet *relationSet = [entity mutableSetValueForKey:relationKey];
 for (id destinationEntityInSet in [relationSet allObjects]) 
 {
 //recursive call
 [self propagateToCascade:destinationEntityInSet];
 }
 }
 //if is a single-entity-relation (is a NSManagedObject)
 else 
 {
 [self propagateToCascade:[relationDescription destinationEntity]];	
 }
 }
}

Good Luck :)

1 nota core data data model ios nsmanagedobject objective-c xcode apple

How to create and use a Custom UIMenuController

I am back… today i will present a new iOS tutorial!

Do you know the UIMenuController?

UIMenuController

Well, in the next tutorial, i will explain how use a UIMenuController with a NSButton (you can use any object which inherits from the UIView class, simply connect the object’s action to the correct method).

One more thing, you can download the project here (created with XCode 4.3.2, iOS 5.1 and Storyboard).

Good luck!

3 note ios objective-c tutorial apple xcode

The new iPad - 4G (LTE) and Retina Display

The new iPad is the best iPad… is the best tablet (it’s much more) in the world.

Well, with the new retina display you can play Games, Video and Apps with a revolutionary resolution. The new iPad is not like a Playstation or a Xbox 360, it’s much more!

This is a screenshot made by the new iPad. It’s fantastic… maybe your monitor is not so good then you can only imagine all this! 

The new iPad - screenshot

“One More Thing”… the 4G or LTE (Long Term Evolution) is a standard for wireless communication of high-speed datas. It’s fast (326Mbps download, 86Mbps upload), it’s new (i don’t know other devices with the 4G technology inside), it’s incredible.

That’s incredible!

iPad Apple