iPhone Development - Very Discouraging.

by 15. December 2008 06:24
I started an iPhone app about 3 weeks ago in the hopes to learn Objective C, keep myself somewhat involved in the actual act of programming, and make a little extra scratch to pay for the monthly cost of this thing.

I came up with this sweet idea. I decided I would do a calculator, but not just a regular calculator. Mine would have the ability to save the calculated result with a note, AS WELL as the ability to save to "formula" (and I use that term loosely) with a single variable in it.

So I learned Obj-C well enough to write the basic calculator. Lemme tell you, the workflow of a calculator isn't as simple as you'd think. Moving on...

So I got the basic calculator working and it was nice. Mine had a few options that the basic iPhone calc didn't, such as % and √. I figured I could sell that basic calc for maybe $1 or just plop it out there for free. Then I am reading reviews for another calc by the same guy who wrote I Am Rich, and a user mentions turning the iPhone calc sideways and it's a scientific.... WTF?! So I did... lo and behold.. it's got like.. a hozillion options... pretty discouraging

*sigh*

So I take 3-4 days off before I finish the other options. Tonight I was going to get back on track and finish that thing before Christmas.

So tonight I find ThinkDigits.   It's pretty much what I was going to do... only a lot prettier. So.. now.. here I sit gazing over my other ideas ... 50% of which already exist.

Wow.. very discouraging. At least I am more smarterer now.

Tags: , ,

Apple | Development | iPhone

Learning and Understanding OOP (Object Oriented Programming)

by 11. December 2008 18:31

OOP is difficult to understand because it’s an abstract concept that doesn’t really apply to anything within the bounds of reality. That may sound really out there, but it’s software, it’s a bunch of jibberish that a computer uses to take input and do something with it.
I have taught several people OOP and it always comes back to The Rocking Chair. I don’t remember where I picked this up, or if I made it up, but read on and you shall understand the basic concept and use of OOP.

“Everything in [c#, Java, etc] is an object…” you’ve heard it a million times. What does that even mean? Well try to visualize it in reality, what is an object.. well , everything is. A ball, a cow, a rocking chair. Let’s stick with a rocking chair.

Classes,  Objects, and Properties
Let start first with a regular chair. We need to look at what makes a chair a chair, what are the properties of the chair. Color, material, height, width, weight, capacity, does it have armrests, a back?  I will use C# to code all this because I think in C#.
To create an object we need something that defines what that object actually is, we use a class or a struct (structure) to define this. So let’s create a chair.
A quick word on properties and methods.. properties are the adjectives, methods are the verbs. Properties hold the information about an object, methods (or functions, routines, messages) do things, they make things happen.

[Chair.cs]


using System;
using System.Collections.Generic;
using System.Text;

namespace UnderstandingOOP
{
    public class Chair
    {
    }
}

Wow, that was exciting.

Now, let’s add the properties of the chair that we recognized above.

[Chair.cs]

using System;
using System.Collections.Generic;
using System.Text;

namespace UnderstandingOOP
{
    public class Chair
    {
        // these are the private members
        // don't worry about these right now
        // these are used so that we can save values
        // and use them in our code.
        private string _color;
        private string _material;
        private int _height;
        private int _width;
        private double _weight;
        private double _capacity;
        private bool _hasArmrests;
        private bool _hasBackrest;

        // these are properties. This is how we will access/set
        // the information about a Chair when we create it
        // these just protect the private members
        // they restrict and enforce the way that a user
        // can interact with the private members
        public string Color
        {
            get { return this._color; }
            set { this._color = value; }
        }
        public string Material
        {
            get { return this._material; }
            set { this._material = value; }
        }
        public int Height
        {
            get { return this._height; }
            set { this._height = value; }
        }
        public int Width
        {
            get { return this._width; }
            set { this._width = value; }
        }
        public double Weight
        {
            get { return this._weight; }
            set { this._weight = value; }
        }
        public double Capacity
        {
            get { return this._capacity; }
            set { this._capactity = value; }
        }
        public bool HasArmrests
        {
            get { return this._hasArmrests; }
            set { this._hasArmrests = value; }
        }
        public bool HasBackrest
        {
            get { return this._hasBackrest; }
            set { this._hasBackrest = value; }
        }

        // this is a constructor, we need it, don’t worry about it.
        public Chair() { }  

    }
}

Alright. So now we have something that defines what a chair is. So let’s create an object from that definition.

[myAwesomeClass.cs]

using System;
using System.Collections.Generic;
using System.Text;

namespace UnderstandingOOP
{
    public class myAwesomeClass
    {
        // lets pretend this is the entry point of
        // our application... where the application
        // starts executing from.
        public static void Main(string[] args)
        {
            Chair myChair = new Chair();

            // lets assume height and width are in feet,
            // weight is in lbs, and capacity is people
            // that can sit in it.
            myChair.Color = "green";
            myChair.Material = "nerf";
            myChair.Height = 5;
            myChair.Width = 7;
            myChair.Weight = 100;
            myChair.Capacity = 3;
            myChair.HasArmrests = true;
            myChair.HasBackrest = true;
        }
    }
}

Awesome.. so we defined what a chair was, in our class, then we created an object based on that definition. We then set all the properties of that object to what we wanted so that we came out with a big green nerf chair.

Inheritance and Polymorphism (don’t be scared, that word means less than it sounds like)
Now that we have a chair, we can create a derivative of that chair. By using the basic OOP concept of Inheritance we can create another type of chair without having to redefine the underlying structure.. which is a basic chair. Rocking Chair needs to inherit everything that a Chair is and does.

public class RockingChair : Chair {}

TADA. We now have defined that a Rocking Chair is exactly the same as a chair. Using the above example you could now do RockingChair myChair = new RockingChair(); . But that’s not quite accurate is it.. a rocking chair does a lot more than a regular chair … plus is HAS TO have a back.
So we can extend the functionality of a Rocking Chair.

[RockingChair.cs]

using System;
using System.Collections.Generic;
using System.Text;

namespace UnderstandingOOP
{
    public class RockingChair : Chair
    {
        // again.. private members
        // they are here to keep things in sync between the methods
        private bool _isRocking = false;
        private int _rockingSpeed = 0;

        // These are public methods. A method is a verb,
        // it’s an action that your object is going to perform
        // A property is a noun, they don’t DO anything,
        public void BeingRocking()
        { 
            // whatever code would make a chair rock
        }

        public void StopRocking()
        { 
            // whatever code would make a chair stop rocking
        }

        public void RockFaster()
        { 
            if(this._isRocking && this._rockingSpeed <= 10){this._rockingSpeed++;}
        }

        public void RockSlower() {  if(this._isRocking && this._rockingSpeed >= 1){this._rockingSpeed--;} }

 // property is now read only.. so it can’t be changed;
        public bool HasBackrest{get{ return true;}} }
}

So using the example above…
[myAwesomeClass.cs]

using System;
using System.Collections.Generic;
using System.Text;

namespace UnderstandingOOP
{
    public class myAwesomeClass
    {
        // lets pretend this is the entry point of
        // our application... where the application
        // starts executing from.
        public static void Main(string[] args)
        {
            RockingChair myChair = new RockingChair();
            // sweet now we have a RockingChair object!
            // lets make our chair look cool by setting the properties

            // lets assume height and width are in feet,
            // weight is in lbs, and capacity is people
            // that can sit in it.
            myChair.Color = "green";
            myChair.Material = "nerf";
            myChair.Height = 5;
            myChair.Width = 7;
            myChair.Weight = 100;
            myChair.Capacity = 3;
            myChair.HasArmrests = true;
            // myChair.hasBackrest =true; we can’t set this anymore, it’s always true.

            // ok make it rock…
            myChair.BeingRocking();
            myChair.RockFaster();
            // loop it to rock SUPER FAST!
            for (int i = 0; i < 10; i++) { myChair.RockFaster(); }
            //ok.. stop the rocking!
            myChair.RockSlower();
            myChair.StopRocking();

            // methods always have to have () after them, sometimes
            // you will pass parameters in the () like (“myName”, 1,2,3)
            // etc.. but for methods like ours with no parameters, the ()
            // is still required.  Properties DO NOT require () as they
            // are not methods and do not have parameters.
        }
    }
}


Awesome! So.. now you understand… difference in a class and an object, difference in a property and a method, and what they do, and the basic concept of inheritance!
Let’s look at polymorphism. Polymorphism is a complicated way of saying that you can create an instance of a class (object) and assign it to the base object. Weird… yeah.. here’s an example…

Chair myChair = new RockingChair()  ….   Spectacular. Not really. That’s polymorphism…

You could conceivably create an array of Chairs and add all kinds of different chairs to it (Rocking Chairs, bar stools, office chairs, etc) and it would be fine. That’s a really basic use of polymorphism.  Wikipedia has a really good description of it.

Getting good at OOP will help you to write code quickly and reuse code as much as possible, even use code between applications. This was a very basic overview to help you understand what an object is, how it relates to a class, what a method and property is, and what inheritance and polymorphism are.
I hope it helped, email me or comment if you have questions!

All the code for this example is attached and downloadable, it's a VS 2005 project. Enjoy!

OOPChair.zip (21.92 kb)

Tags: ,

Development | How To

the iPhone

by 9. December 2008 08:43

I was going to write a big long jibba-jabba about my iPhone, but I'm not cause that's stupid and by now you've probably seen a dozen of them. Let me underscore this.. they're awesome ... really awesome.. more awesome thatn you can probably imagine.

 Shortly after I got it (early Christmas present from the wife) I instantly decided that just having isn't good enough, I need to be complicated and start writing code for it. I got the SDK and have been trucking along.

 I am learning Objective C, which I have to say is a lot different than C# and as usual, "desktop" programming is a lot different than web development. There's a lot of oddities about obj-c as well. member variables don't really work as you'd expect, neither do bools. I've found all kinds of odd things.

 Anyway, I am really enjoying it. I am hoping to have my app up in the next few weeks. When it's done I will put up some code and stuff.

Here's some helpful links if you are starting out...
Apple's Developer Site
Wikipedia Entry on Obj-C
iPhone book... great price

Tags: , ,

Apple | Development


RecentComments

Comment RSS