This one is for you, Ben.

I have been going through some interviews lately and feel kinda dumb being asked technical questions on the spot. I actually felt really really dumb today after this particular one. So for those of you junior developers out there here is a tip.
This is what I said:

function reverseString(string){
var holder = new Array();
for(var i = string.length-1; i > -1; i--){
holder.push(string.length[i]);
}
return holder;
}

You would ONLY use this in a case when you are going to be doing something with the original string later in the same function. The more effective way would be to use this code:


function reverseString(string){
var swap = '';
var mid = Math.floor(string.length/2);
for(var i = 0; i < mid; i++){
swap = string[i];
string[i] = string[string.length - i];
string[string.length - i] = swap;
}
return string;
}

This function actually destroys the original string but that’s fine because if they need the string later they can store this call into a new variable like so:
var string = "foo";
var reversedString = reverseString(string);

This is where us developers need to look at the code and decide what is better: Faster code or more readable code?

A thank you to Ben for opening me up to a new way of thinking and for making me think about efficiency. 😀

PHP backend abstraction to create controllers

I need to change the title of the blog to just be some sort of generalized programming blog. Anywho, I took a class on php and backend development to make a dynamic website and I found this nifty little trick. I came up with this on my own and had no references on it but I guess it is called abstraction. Start by inserting all your web page information inside the database with a page id, title, and content. Add this code to your index page or controller.

/* GENERATES ALL THE CONTROLLERS FOR ALL THE APPS*/

$app = apps();

foreach($app as $ap){

if ($action == $ap[‘pid’]) {

$page = getApp($action);

$title = $page[‘title’];

$content = $page[‘content’];

$view = “view.php”;

} }

Then in the model you just query the database using a function like this:

////////////////////////////////////// GET EVERYTHING FROM THE APPS TABLE

function apps() {

$link = connect();

try{ $query = “Select * from apps”;

$stmt = $link->prepare($query);

$stmt->execute();

$result = $stmt->fetchAll();

} catch(PDOException $e) {

$result = false; }

return $result; }

This will return all of your webpages inside the apps table inside a PDO object which the index loops through and creates those controllers for. I suppose this is a very simple version of abstraction but I thought it was pretty cool that I figured this out on my own. It also saves a lot of duplicate code off the page. If you had several tables of pages you could probably do a cross join to get all the web pages and create all the controllers for everything.

Ios 8 contacts missing bug

I downloaded the Ios 8 beta fro the developer site. Everything was going great then about a day ago apps would just close upon being opened, I started missing text messages, and all the names of my contacts disappeared. If you happen to run into this “bug” here is how to fix it. 

Connect your iPhone to your computer with itunes. Click on your device. In the bottom right corner of itunes there should be a sync button. Sync your iPhone. After the sync is finished unplug the iPhone and reboot it by holding the power button for about 5 second. Tune the phone back on and when it turns back on it should ask for your credentials. enter your credentials and it should bring back your contacts and the phone will run much smoother. 

 

Good luck.

I know I know.

I started this blog to go along with the book that I had bought to learn iOS programming. A lot has happened and I got caught up in some other things. I had another project that I had wanted to finish up and started a few more because I got the idea and started programming. I just finished up one that I had. I learned about Singleton Methods. Basically a singleton is where you create a object that can be accessed globally. 

What I did was create a new file inside the existing project that I had. Named it global.h,global.m. 

In the global.h put this. 

#import <Foundation/Foundation.h>

@interface globalVariables : NSObject{
int globalInt;
}
@property(nonatomic) int globalInt;

+(globalVariables *) singleObj;

@end

in the global.m put this. 

#import “global.h”

 

@implementation global{

global * anotherSingle;

}

 

@synthesize globalInt;

 

+(global * )singleObj{

static global * single=nil;

    @synchronized(self){

if(!single){

            single = [[global alloc] init];

        }

    }

return single;

}

 

 

@end

This will allow you to #import “global.h” and create variables inside it and store them as globals. I found this very helpful when passing variables between views. 

 

Finished my project.

Just finished up my project I was having trouble with. I figured out how to add a banner view on top of a web view without covering up content on the page. It is pretty neat.

//THIS IS YOUR .H FILE OF YOUR WEBVIEW

@interface DetailViewController : UIViewController<UIWebViewDelegate, ADBannerViewDelegate>{

ADBannerView *adView;

}

This creates the delegates for your web view and banner view and creates an adbannerview called adView. Add these 2 methods to your .M file.

– (void)bannerViewDidLoadAd:(ADBannerView *)banner

{

CGRect resizedWebView = _webView.frame;

resizedWebView.size.height = adView.frame.origin.y;

_webView.frame = resizedWebView;

[self.view bringSubviewToFront:adView];

adView.hidden = NO;

}

– (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

{

CGRect resizedWebView = _webView.frame;

resizedWebView.size.height = self.view.frame.size.height;

_webView.frame = resizedWebView;

adView.hidden = YES;

}

Then add this to the .M webViewDidFinish

– (void)webViewDidFinishLoad:(UIWebView *)webView{

adView = [[ADBannerView alloc] initWithFrame:CGRectZero];

CGRect adFrame = adView.frame;

adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;

adView.frame = adFrame;

[self.view addSubview:adView];

}

This will create a adbanner at the bottom of the web view and it shrinks the web view so that nothing is covered on the bottom. Enjoy!

Personal project before starting book

So I have had a few iPhone app projects that I was working on that I thought I would have finished earlier so I could begin working through this book. Things are taking a bit longer than expected because I am very green at this. 

I have been following tutorials on making RSS Feed readers which has worked out swimmingly. The issue i had run into was fitting iAds banners into the UITableViewControllers. This would have been much easier had I found this book and built the app from the ground up instead of using a pre programmed “Master-Detail” layout provided by Xcode. The issue I was running into was getting the ads to show up at the bottom of the table view. I found some code on a post from Stack Overflow which allowed me to move the banner to the bottom of the table as a subview and therefore display over the table view. That was pretty nifty. 

  //LAYS THE AD BANNER ON TOP OF THE TABLE

//ADD THIS CODE TO YOUR “VIEWDIDLOAD” METHOD INSIDE THE .M FILE

    if (!tableView && [self.view isKindOfClass:[UITableView class]]) {

        tableView = (UITableView *)self.view;

    }

    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

    self.tableView.frame = self.view.bounds;

    [self.view addSubview:self.tableView];

    //self.banner.frame = CGRectMake(0, self.view.frame.size.height – 50, 320, 50);

    self.view.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:banner];

    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);

And then make a new method that repositions the banner upon scrolling

– (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //refresh banner frame during the scrolling

    CGRect bannerFrame = self.banner.frame;

    bannerFrame.origin.y = self.view.frame.size.height – 50;// + self.tableView.contentOffset.y;

    self.banner.frame = bannerFrame;

}

The piece that I have commented out on the bannerFrame.origin.y allows the banner to scroll up or down based on the user actions, which is super cool. If you leave it commented out the way I have it here it repositions the banner to the bottom of the tableview. There is one big problem with this and that is that it only repositions the banner AFTER the user has scrolled. 

I just need to figure out how to generate the banner into this position upon loading the view but I have messed around with it for several hours now with no progress.  Really cool code for scrolling banners though. 

After I finish this up in the next day or two I will begin running through the book.

Starting Out

I have never run my own blog before but I have been advised by someone I have outstanding respect for to start a blog. I will be covering things I am currently learning about Objective-C programming and IOS Development.

So far I have been working on a few projects of my own and learning from googling everything. Most of this blog will cover my experience in working through a book that has been recommended to me called “iOS Programming: The Big Nerd Ranch Guide (4th Edition)”. You can buy it from amazon here.

So far by working on my own I have made a soundboard and a simple Magic The Gathering life calculator w/ poison counters and just updated it to include storm counters. I am currently working on a RSS Feed reader and I will include information about that when it is finished.