Category Archives: Coding - Page 2

generate doctrine modules + setters and getters

vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping annotation module/Application/src/ --namespace="Application\\Entity\\" --from-database --force
vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities --update-entities="true" --generate-methods="true" module/Application/src/

Split a string literal across multiple lines in C / Objective-C

When using long literal strings in you’re code it’s nice to split them up among multiple lines.
To do this the syntax is like this:

In C:

char *my_string = "Line 1 \
                   Line 2";
char *my_string = "Line 1 "
                  "Line 2";

In Objective-C:

NSString *my_string = @"Line1 \
                        Line2";
NSString *my_string = @"Line1" 
                       "Line2";

I personally like the second  options with all quoted nicly;

Dismiss Keyboard on enter

It’s a common thing in iOS to hide the keyboard on a enter press when filling in some text fields. This is easily accomplished by adding the following code snippet to you’re project.

 

First add the UITextFieldDelegate to your controller view.

@interface myViewController : UIViewController  <UITextFieldDelegate>

 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if([string isEqualToString: @"\n"]){
        [textField resignFirstResponder];
    }
    return YES;
}

Encode and Decode GPS coordinates into a String

Encode GPS location into a name.
Or do the reverse.
GeoEncode.h

//
//  GeoEncode.h
//  Open Industries
//
//  Created by Utopia on 11/21/11.
//  Copyright (c) 2011 code.toemaar.nl. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
 
@interface GeoEncode : NSObject {
    NSString *_apiUrl;
}
 
- (NSDictionary *) encodeString :(NSString *) locationStr;
- (NSString *) decodeLatLng :(CLLocationCoordinate2D) location;
- (void) setNetworkActivity :(BOOL) show;
@end

GeoEncode.m

//
//  GeoEncode.m
//  Open Industries
//
//  Created by Utopia on 11/21/11.
//  Copyright (c) 2011 code.toemaar.nl. All rights reserved.
//
 
#import "GeoEncode.h"
#import "SBJson.h"
 
@implementation GeoEncode
 
- (id) init {
    if ( self = [super init] ) {
        _apiUrl = @"http://maps.googleapis.com/maps/api/geocode/json?sensor=false";
    }
    return self;
}
 
- (NSDictionary *) encodeString :(NSString *) locationStr {
    NSDictionary *googleResult;
    NSMutableDictionary *result, *addressDict, *locationDict;
    NSError *error = nil;
    CGFloat lng, lat;
    NSString *address, *status;
 
    [self setNetworkActivity :YES];
 
    NSString *urlStr = [[
                            [NSString alloc]
                                initWithFormat:@"%@%@%@", _apiUrl, @"&amp;address=", locationStr]
                                    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlStr] encoding:NSUTF8StringEncoding error:&amp;error ];
    googleResult = [jsonStr JSONValue];
    if(0 == error.code){
        if([@"OK" isEqualToString: [googleResult objectForKey: @"status"]]){
            addressDict = [[googleResult objectForKey: @"results"] objectAtIndex:0];
            locationDict = [[addressDict objectForKey:@"geometry"] objectForKey:@"location"];
            address = [addressDict objectForKey: @"formatted_address"];
            lng = [[locationDict objectForKey: @"lng"] floatValue];
            lat = [[locationDict objectForKey: @"lat"] floatValue];
            status = [googleResult objectForKey: @"status"];
        }else{
            lng = 0.0;
            lat = 0.0;
            address = @"";
            status = [googleResult objectForKey: @"status"];
        }
    } else {
        lng = 0.0;
        lat = 0.0;
        address = @"";
        status = @"";
    }
 
    CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
    result = [[NSMutableDictionary alloc] init];
    [result setObject:location forKey:@"location"];
    [result setObject:address forKey:@"address"];
    [result setObject:status forKey:@"status"];
    [self setNetworkActivity :NO];    
 
    return [NSDictionary dictionaryWithDictionary:result];
}
 
- (NSString *) decodeLatLng :(CLLocationCoordinate2D) location {
    NSString *result = @"";
    NSError *error = nil;
    NSDictionary *googleResult, *addressDict;    
 
    NSString *urlStr = [[NSString alloc] initWithFormat:@"%@%@%f,%f",  _apiUrl, @"&amp;latlng=", location.latitude, location.longitude];
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlStr] encoding:NSUTF8StringEncoding error:&amp;error ];     
 
    if(0 == error.code){
        googleResult = [jsonStr JSONValue];
        if([@"OK" isEqualToString: [googleResult objectForKey: @"status"]]){
            addressDict = [[googleResult objectForKey: @"results"] objectAtIndex:0];
            result = [addressDict objectForKey: @"formatted_address"];
        }
    }
 
    return result;
}
 
- (void) setNetworkActivity :(BOOL) show {
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = show;
}
 
- (void)dealloc {
    [_apiUrl release];
    _apiUrl = nil;
}
 
@end

Objective-C Singleton

Header file (MySingleton.h)

#import <UIKit/UIKit.h>
 
@interface MySingleton : NSObject  {
}
 
@end

Implementation File (MySingleton.m)

#import "MYSingleton.h"
 
@implementation Mysingleton
    static Idea *_instance = NULL;
 
+ (MySignleton *) getInstance {
    @synchronized(self) {
        if (NULL == _instance)
            _instance = [[self alloc] init];
    }
 
    return _instance;
}

Nice, plain and simple. But i’m always open for suggestions.