Random programming things I'd want to remember

Wednesday, February 29, 2012

Changing current directory with NSFileManager




NSFileManager *fm;

fm = [NSFileManager defaultManager];


if ([fm changeCurrentDirectoryPath: @"/Users/yourUserNameHere/Documents/folder1/folder2"] == NO)
   NSLog (@"Something went wrong");
//if you don't see the "Something went wrong" message, then the current directory has beed changed, here is how to check it:

NSLog(@"%@", [fm currentDirectoryPath]);




Taking screenshots on a Mac

Tuesday, February 28, 2012

Where XCode keeps compiled files

If using XCode (4.2) and needing to work with path-sensitive code, here is how you can find out where the executables are kept:

File -> Project Settings...

Then click the little arrow by the path under "Derived Data Location" (closer to Advanced button in XCode 4.2). There you will see folders with compiled files. If needed to add some path-sensitive data, insert it under the following path:

Project Name + some gibberish/Build/Products/Debug

Monday, February 27, 2012

Compile Objective-C code From Terminal With ARC Using Clang

In order to compile Objective-C in the terminal, here is what needs to be done: 1. Open Terminal window 2. If using XCode to get the autocompletion and syntax highlighting, navigate to the project folder (using "cd" commands to change directory and "ls" command to see what's in the current folder; use "cd .." to go up one level if went somewhere wrong). 2. If using terminal to write code as well, open vi and type in the code. 3. Enter the following line of code at the command line while the terminal window is in the same folder as your .m and .h files:
clang -fobjc-arc -framework Foundation main.m -o yourAppName

This will compile the code into an executable file. To run, type
open yourAppName

at the command line. Alternatively, you can run your program by typing the program name preceded with ./ like so:
./yourAppName

Or you can add the name and path to your program to the PATH variable of your system.

Sunday, February 26, 2012

Objective-C: String to number to object conversion notes

So you need to add a double into an NSMutableArray, but you cannot do it directly because double is not an object. Here is how you wrap a double into an object type of NSNumber and add it to the array:
//create and initialize array:
NSMutableArray *arr = [NSMutableArray array];

//add your double to the array:
[arr addObject:[NSNumber numberWithDouble:yourNumber]];

//Here is how you unwrap it back into a double:
NSNumber *someNumber = [arr lastObject];

double d = [someNumber doubleValue];

Here is another handy trick, it turns out that NSString has a method that tries to return a double from the string itself. Here is how you write it:
NSString *myString = @"5.0";
double result = [myString doubleValue]; //I bet there is also a method to return an integer in the same manner

Here is how a double is converted to an NSString object:
NSString *resultString = [NSString stringWithFormat:@"%g", result];

Saturday, February 25, 2012

Objective-C, display object info using %@ format

If needed to display the object info in an informative manner using %@ format, implement the "description" method, like so:

-(NSString *) description
{
   return [NSString stringWithFormat: @"%i/%i/%i", _month, _day, _year];
}


The idea is the same as, for example, overriding ToString() method in C#.

Thursday, February 23, 2012

Create new object with some default parameters and NSMutable array to hold items


@interface Playlist : NSObject
@property (nonatomic, copy) NSString *playListName;
@property (nonatomic, strong) NSMutableArray *songs;

-(void)addSong:(Song *) aSong;
-(void)removeSong:(Song *)aSong;
-(id)initWithPlayListName:(NSString *)newPlayListName;
@end

//#import "Playlist.h"

@implementation Playlist
@synthesize playListName = _playListName, songs=_songs;

-(void)addSong:(Song *)aSong
{
    [_songs addObject:aSong];
}

-(void)removeSong:(Song *)aSong
{
    [_songs removeObject:aSong];
}

-(id)initWithPlayListName:(NSString *)newPlayListName
{
    self = [super init];
    if(self)
    {
        _playListName = newPlayListName;
        _songs = [NSMutableArray array];
    }
    
    return self;
}
@end

Objective-C tidbits

Sort an NSArray of NSIntegers:

NSArray *sorted = [arr sortedArrayUsingSelector:@selector(compare:)];


Iterate over items in NSCountedSet:

for(id i in set)
{
      NSLog(@"Item %@, count: %lu", i, (unsigned long)[set countForObject:i]);
}