xcode

Find all combinations with repetitions (Objective-C)

Posted on Updated on

Today after retouching on Objective-C for a few hours (from this Youtube video: https://www.youtube.com/watch?v=5esQqZIJ83g) I decided to recreate my C++ program that prints out all possible combinations of ABC (e.g. AAA, AAB, AAC, ABA … CCC) which I posted about here ( https://kagato0110.wordpress.com/2015/10/28/find-all-combinations-with-repetitions-c/ ) in Objective-C as a terminal based app.

Which was really fun to do! Wasn’t hard to do actually, pretty simple^^

Header file: GenerateSequence.h

/**
 *  GenerateSequence.h
 *
 *  Created by Benjamin Lo on 2015/10/30.
 *  Copyright (c) 2015年 BAD Games (Benjamin Lo). All rights reserved.
 */
#import <Foundation/Foundation.h>

@interface GenerateSequence : NSObject

@property NSMutableArray *_sequence;

- (void)         AssignValueForSequence;                            //  Begin letter sequence
- (void)         CheckIfEmpty       : (NSMutableArray *) _sequence; //  Check if array is empty
- (void)         GenerateSequence   : (NSMutableArray *) _sequence; //  Begin combinatorial sequence
@end

Implementation file: GenerateSequence.m

/**
 *  GenerateSequence.m
 *
 *  Created by Benjamin Lo on 2015/10/30.
 *  Copyright (c) 2015年 BAD Games (Benjamin Lo). All rights reserved.
 */
#import "GenerateSequence.h"

@implementation GenerateSequence


/**
 * Initialization
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        //  Do nothing lol
    }
    return self;
}


/**
 *  Begin letter sequence
 */
- (void) AssignValueForSequence
{
    NSMutableArray *_sequence = [NSMutableArray arrayWithCapacity: 3];

    [_sequence addObject: @"A"];
    [_sequence addObject: @"B"];
    [_sequence addObject: @"C"];
    
    [self CheckIfEmpty:_sequence];
}


/**
 *  Check if array is empty
 */
- (void) CheckIfEmpty: (NSMutableArray *) _sequence
{
    if (![_sequence isEqual: nil])
    {
        [self GenerateSequence:_sequence];
    }
}


/**
 *  Begin combinatorial sequence
 */
- (void) GenerateSequence : (NSMutableArray *) _sequence
{
    NSMutableString *_a;
    NSMutableString *_b;
    NSMutableString *_c;
    
    for (int i = 0; i < [_sequence count]; i++)
    {
        for (int j = 0; j < [_sequence count]; j++)
        {
            for (int k = 0; k < [_sequence count]; k++)
            {
                _a = [NSString stringWithFormat:@"%@", [_sequence objectAtIndex:i]];
                _b = [NSString stringWithFormat:@"%@", [_sequence objectAtIndex:j]];
                _c = [NSString stringWithFormat:@"%@", [_sequence objectAtIndex:k]];
                NSLog(@"%@%@%@", _a, _b, _c);
            }
        }
    }
}

@end

Main file: main.h

/**
 *  Main.m
 *
 *  Created by Benjamin Lo on 2015/10/30.
 *  Copyright (c) 2015年 BAD Games (Benjamin Lo). All rights reserved.
 */
#import <Foundation/Foundation.h>
#import "GenerateSequence.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
//        GenerateSequence *gs = [[GenerateSequence alloc] initWithName:@"Generate sequence"];
        GenerateSequence *gs = [[GenerateSequence alloc] init];
        [gs AssignValueForSequence];
    }
    return 0;
}

You can git clone this from my Github: https://github.com/benji011/ABC_CombinationSequence/commits/master

Happy coding!

Installation Failed: Invalid Argument – iOS Simulator & XCode 6.4

Posted on Updated on

I had this error since yesterday and couldn’t figure out what was causing this.

Tl:dr – Check your Resources folder in your project folder and make sure you don’t have copies. Then add them properly into your workspace in XCode.

The detailed version:
My initial thoughts was my Bundle name, identifier and product name were different but changing either wouldn’t work. But then I ran into this thread:

https://forums.developer.apple.com/thread/7338

Update: Xcode doesn’t like it when you have a referenced directory called ‘Resources’ within your resource bundle.. but instead of complaining about this, you end up wasting half the day trying to resolve all the other issues that it throws up at you instead (see previous two posts).

All is now resolved and working as it should.

Looking at my work tree I had 2 Resources folders. One under my project folder and another inside my proj.ios_mac folder. Not only that I had the contents of my Resources folder copied within proj.ios_mac!

Deleting these copies, selecting “Add files to [project]” eventually did the trick.

Fun with Objective-C

Posted on

Last week I had the chance to look at my Sempais amazing new iOS app he built on his own which he did for fun. And let me tell you, it was gorgeous. It was far from complete, but what he wrote was an app that would show a mobile version of a browser interface. That interface has his bookmarks neatly organized into specific categories on Chrome.

I thought “wow, that’s amazing! I want to build something like this too” so I decided to have a go at Objective-C this weekend.

This … isn’t a tutorial at all but I thought for self reminder I should put this here.

This link ( https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html ) is my reference to building a simple To-Do list by Apple. The problem was last night I had written this code to add my text objects into the list through this method:

-(void)loadInitialData {
    ToDoItem *item1 = [[ToDoItem alloc] init];
    item1.itemName = @"Buy milk";
    [self.toDoItems addObject:item1];
    
    ToDoItem *item2 = [[ToDoItem alloc] init];
    item2.itemName = @"Buy eggs";
    [self.toDoItems addObject:item2];
    
    ToDoItem *item3 = [[ToDoItem alloc] init];
    item3.itemName = @"Read a book";
    [self.toDoItems addObject:item3];
    
}

However when I build this, I get an error something like “NSMutableArray exception” ..
looking through the docs ( https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/#//apple_ref/occ/instm/NSMutableArray/addObject: ) it says that anything addObject gets thrown which is Nil would throw an exception. So the problem mean’t that what I was adding to my array was either of NULL value or my declarations are wrong.

After checking through what I had written last, I discovered this morning my problem was here in this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    // catch approproate item in the toDoItems array and set the name of that item to display in the cell
    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;
}

My placeholder was declared in the source as @”reuseIdentifier”, which doesn’t exist . In my Main.Storyboard, under To-Do-List scene > Table View > my cell was named @”reuseIdentifier”.

A quick modification from above to the following method would then insure that the compiler knows where and what to do with my passed data:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
    ToDoItem *toDoItem =[self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;
}

Hitting build would now display my app without errors.