Testing objective-c

Discussion in 'iOS Programming' started by Evre, Jul 8, 2012.

  1. Evre macrumors newbie

    Joined:
    Nov 5, 2011
    #1
    What is a good way to test objective-c code? I don't know anything about programming, although I did write a few Qbasic programs when I was younger. I'm reading a beginners tutorial at cocolab.com and it gives examples of simple code. Is there a way of testing this code out and see what the result is? Sorry I'm completely new. I also just got the cheapest mac mini for the sole purpose of development. It seems like a daunting task but I have a lot of ideas. Is this a good forum for new people?
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    You should get Xcode from the Mac App store, it's free download.
    You can compile code in there ^_- (on the simulators).
     
  3. Evre, Jul 8, 2012
    Last edited by a moderator: Jul 8, 2012

    Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #3
    Thanks! How do I make an app which displays a message? I went into main.m

    and put the Nslog after all the code that was already there but when when I ran the simulator it was blank.
    Code:
    
    //
    //  main.m
    //  test3
    //
    //  Created by Imran Ganiy on 7/8/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    #import "AppDelegate.h"
    
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
            
            NSLog(@"Hi.");
            return 0;    
            
        }
    }[COLOR="#808080"]
    
    ----------
    
    [/COLOR]Okay I deleted all the other code and just kept 
    
    int main()
    {
        NSLog(@"Hi.");
        return 0;
    }
    
    but now the app just immediately closes after I run it.[COLOR="#808080"]
    
    ----------
    
    [/COLOR]I copy and pasted this code from the guide and the app doesn't run but it also says no issues.  I put it in main.m
    
    #import <Foundation/Foundation.h>
    float circleArea(float theRadius);
    float rectangleArea(float width, float height);
    int main()
    {
        float pictureWidth, pictureHeight, pictureSurfaceArea,
        circleRadius, circleSurfaceArea;
        pictureWidth  = 8.0;
        pictureHeight = 4.5;
        circleRadius  = 5.0;
        pictureSurfaceArea = rectangleArea(pictureWidth, pictureHeight);
        circleSurfaceArea = circleArea(circleRadius);
        NSLog(@"Area of circle: %10.2f.", circleSurfaceArea);
        NSLog(@"Area of picture: %f. ", pictureSurfaceArea);
        return 0;
    }
    float circleArea(float theRadius)               // first custom function
    {
        float theArea;
        theArea = 3.1416 * theRadius * theRadius;
        return theArea;
    }
    float rectangleArea(float width, float height) // second custom function
    {
        return width*height;
    }


    ----------

    Now I tried combining the original code with the example code and the screen is still white with no printed images

    Code:
    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import <Foundation/Foundation.h>
    
    float circleArea(float theRadius);
    float rectangleArea(float width, float height);
    
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
           
            float pictureWidth, pictureHeight, pictureSurfaceArea,
            circleRadius, circleSurfaceArea;
            pictureWidth  = 8.0;
            pictureHeight = 4.5;
            circleRadius  = 5.0;
            pictureSurfaceArea = rectangleArea(pictureWidth, pictureHeight);
            circleSurfaceArea = circleArea(circleRadius);
            NSLog(@"Area of circle: %10.2f.", circleSurfaceArea);
            NSLog(@"Area of picture: %f. ", pictureSurfaceArea);
            return 0;
            
        }
    }
     
  4. Simplicated macrumors 65816

    Simplicated

    Joined:
    Sep 20, 2008
    Location:
    Waterloo, Ontario, Canada
    #4
    The reason why you are seeing a white screen is that everything you typed only outputs to the console, not the user interface. The console, which is only displayed when the app is running, is located at the bottom of Xcode. The file "MainStoryboard.storyboard" (assuming you checked the option to create it when you created the project) should be found in the Project navigator and that's where the user interface is documented.

    I would suggest you get a copy of Programming in Objective-C 2.0 (3rd or 4th edition will do) by Steven Kochan to familiarize yourself with the programming language and object-oriented programming, and then subscribe to the (free!) CS193p course, called Developing Applications for iOS on iTunes. :) (This will take you around 3-4 months, but will totally be worth the effort!)
     
  5. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #5
    Okay I bought that book.

    Meanwhile, please help me figure this out.

    I see no mainstoryboard.storyboard file anywhere in Xcode, and I recreated a new project and there was no option to create it.

    Where do I put the code if I want to print a message on the screen?

    There are a bunch of files, AppDelegate.m, Jaspertests.m, main.m

    I have no idea what any of these are or what they do. But I read a little on cocoa labs and understand how to write a bit of code. I just need to know how to test it out and see the results in the simulator so I can experiment and learn.

    My project is called "Jasper"

    I appreciate the time you take to respond! Thanks again.

    ----------

    Oh I changed the color, it's a green background now instead of white. Kind of pointless, but I made a visible change!
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    You're getting way ahead of yourself.

    If you'd like to write iOS programs, you're going to need to learn the following:
    1 - The C Programming Language
    2 - The Obj-C Programming Language (don't worry, C blends seamlessly into it.)
    3 - How to use Xcode
    4 - The iOS SDK

    I suggest you start with the first one all by itself... I hear suggestions to learn 2-4 individually to keep yourself from getting them all mixed up, but I don't really know how to learn any of them without the others.

    So, to learn the first one, I suggest you read this free ebook and do the exercises... you'll want to get up through exercise 17 at a minimum.
    http://c.learncodethehardway.org/book/

    To learn 2-4, I suggest going onto iTunes U and finding Stanford's free courses on iOS programming.

    Good luck
     
  7. Simplicated macrumors 65816

    Simplicated

    Joined:
    Sep 20, 2008
    Location:
    Waterloo, Ontario, Canada
    #7
    I am not sure what template you selected when you created the project, but I tend to create apps using the Single View Application template, that way I can build on top of that single view (and controller) and be able to understand the relationship between each object. There should be the option to create Storyboards if you use this template.

    But OP, as ArtOfWarfare said, you're skipping the basics. You said you've already got the book, so you're off to a good start! :)
     
  8. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #8
    I cancelled the book, because I'm following the free E-book on the link which Artofwarfare posted. And it seems like I need to learn C first anyway.

    But I'm stuck!

    http://c.learncodethehardway.org/book/learn-c-the-hard-waych2.html

    I wrote the code in gedit and saved it as Jasper.c, but then he asks me to type something to create the program. Am I missing something obvious? Where am I supposed to type those commands? I tried it in the terminal but those were not recognized commands.
     
  9. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #9
    Okay I figured it out. I had to install the command gcc in the Xcode preferences

    ----------

    I don't understand. It made something called a.out, and I can't even run it.

    ----------

    And the original command the guide said to use starting with $ doesn't exist.

    ----------

    I guess I can run it in the terminal if I type /.a.out
     
  10. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Learn C first. If you never have programmed you need to learn the fundamentals of programming. I first wanted to make an app when I started. I then discovered starting in the middle was hard. I then decided I wanted to be a programmer and started learning with C. IT was over a year before I made my first GUI program because I wanted to learn to program first.

    It takes time and discipline. If you rush it you an get lost quickly. When you understand the tools then you will better understand how to write programs.

    I started with "Learn C on the Mac". But there are many good books on C.
     
  11. chown33, Jul 8, 2012
    Last edited: Jul 8, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    To run a.out from the current directory, enter this in a Terminal window:
    Code:
    ./a.out
    
    The ./ is needed because the current directory isn't normally in the path for commands. This is a security precaution.


    When you get errors in Terminal, copy and paste the actual error message in your posted question.

    When you refer to a tutorial, such as "the original command the guide said to use", either provide the actual URL (and indicate exactly where on the web page), or copy and paste the actual text of whatever you're using.

    We can't see your screen. We don't know exactly what you're reading. You need to ask your questions as if we can't see your screen or read your mind.

    Getting Answers

    Precision is important in programming. Your first post says cocolab.com (note spelling), but there are no general tutorials at the website. There is a completely different website, http://www.cocoalab.com/, which does have general tutorials. Precision is important. Copy and paste is your friend.
     
  12. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #12
    Okay I got it. I will learn C then, thanks for letting me know.

    Sorry I was in a hurry. I kind of bought this mac in a whim and wanted to get making apps as fast as possible. It looks like it's going to take a lot more learning and a slower pace than that however.

    Okay so I'm just going to get started with my C learning process,
    I'll post my struggles here and I hope I can get some good answers and feedback from you guys.

    What is the best book or online book on learning C?

    Art of War posted: http://c.learncodethehardway.org/book/

    What does everyone else think?

    In Chapter 2, Exercise One, he says:


    Are those terminal commands? Because mine doesn't understand "$" or "make". I have "gcc" though, and when I use it, it creates a file called "a.out", which is only executable through the terminal. When I try to run it by double clicking it in it's directory OSx doesn't know what it is.

    How do you compile the program to be the original name instead of a.out, and how can you make it ruinable outside of the terminal?

    More importantly, why is he telling me to use characters and commands that don't work?

    There were other things in that chapter I didn't understand:

    Are these all things he is typing in the terminal? Again there is "$" and now "rm" which he never expained. And what's CLFAGS? and what's -Wall?

    How do I run "man 3 puts"?

    I did Qbasic programming when I was a kid, so I'm not entirely new.

    Is he explaining things properly or should I find a different source?
     
  13. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #13
    Figured out how to copile under a different name.

    gcc -c -o (name you wish) (source file).c

    But whenever I compile under a different name and try to run it I get "Permission Denied"

     
  14. Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #14
  15. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #15
    By doing that tutorial did you understand what was happening? Can you not use the tutorial and rewrite that program in a different way?

    To answer your question above. I picked up the book called "Learn C on the Mac" It was a great book and I think I spent about 3 months reading it really slowly and each tutorial I would write my own version of the tutorial so it would sync in.

    Many people read through a book and still don't get it. The more and more I work in programming I am discovering that 2 things need to happen. 1 learning the tools, and 2 knowing how to use the tools to tackle projects and that part only comes with many hours of trial and error.

    I have seen posts here saying something to the effect "I have read the book, now how do start my app?". I am going on 2 years now and have an app in the app store but I consider my self still new and still learning lots everyday.
     
  16. Evre, Jul 10, 2012
    Last edited by a moderator: Jul 11, 2012

    Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #16
    Okay I agree with you that this is going to take a lot of work, and I already think it's fun so that's no problem. This is something I rewrote and added to from the tutorial I was following. I thought it would be best if I put my questions directly into the code. Thanks to anyone who wants to reply! :)

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int aligator;  /*Why does it fail to compile if I try to use float instead of int?*/
    	int aligator2;
    	
    	/* prompts two inputs, then divides the first by the second */
    	
    	printf( "PLease enter a number: " );
    	scanf ("%d", &aligator ); /*what is %d and why does it have to be there along with the variable?*/
    	printf( "Now enter the number you wish to divide by: ");
    	scanf ("%d", &aligator2 );
    	printf( "%d", aligator/aligator2);
    	
    	/* Just a silly way of testing the if and else function */
    	
    	if (aligator/aligator2>100) 
    	{printf("Number too big");}
    	else {printf("Number too small");}
    	
    	getchar();
    	/*Still not exactly sure what getchar does*/
    	
    	return 0; 
    	/*What if I didn't include the return 0?*/
    }
     
  17. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #17
    The %d in the scanf line is called a Format Specifier. It tells scanf to read an integer, not an float, which might be the problem you have.

    Looks like you would benefit from a C book to help you get going from the start. The good thing is you are having fun so it might be a good idea to get a good book and start on Chapter 1 page 1.

    I really liked 'Learn C on the Mac'. But like Art said above is probably a good book too.

    1. Don't rush it.
    2. Redo all tutorials in your own way.
    3. When you finish a C book write some C programs before you move on.

    Many people finish reading books but don't apply what they learn and can't write a simple C program. I always say "Write a console based Blackjack program." That is what I did and it was very tricky as my first large project. The important thing is to understand how logic works. Once you grasp that Objective C will be easier to understand.

    Interesting story here. I took a Java class at city college. 1/3 of the class came from a beginning programming class to further there programming education. 2/3 had never taken a programming class before and wanted to learn (they started in the middle). The teacher said "Almost all of you that are taking this class that has not taken a basic programming class will drop on the next month. Almost all of you that have taken a programming class will succeeded." He was right 2/3 of the class dropped after the first month.
     
  18. Evre, Jul 11, 2012
    Last edited by a moderator: Jul 11, 2012

    Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #18
    I don't understand why this doesn't work:

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	int age;
    	
    	printf( "Enter your age: ");
    	scanf( "%d", &age );
    	
    	if (age ! = 100) {
    	printf ("You are not 100");
    	}
    	
    	   return 0;
    	}
    All I get is:

    Code:
    Imrans-Mac-mini:Jasper imranganiy$  gcc elseif.c
    elseif.c: In function ‘main’:
    elseif.c:11: error: expected ‘)’ before ‘!’ token
    That error makes no sense. If I put ")" before ! it makes it even worst.
     
  19. Evre, Jul 11, 2012
    Last edited: Jul 11, 2012

    Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
  20. Evre, Jul 11, 2012
    Last edited by a moderator: Jul 11, 2012

    Evre thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #20
    Last problem of the day:

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y);
    
    int main()
    {
    	int x;
    	int y;
    	
    	
    	printf( "Please enter two numbers to be multiplied: " );
    	scanf( "%d", &x );
    	scanf( "%d", &y );
    	
    	/*There is something wrong with the below line.  How can I fix this?  I want it to check to see if x * y = 25 and then print a message
    	if so */
    	
    	/*from here*/
    	if (mult (x, y) = 25) 
    	{ 
    	printf( "You entered 5 times 5" ); }
    	/*to here*/
    	
    	else {
    	
    	printf( "The product of your two numbers is %d\n", mult (x, y) );
    	
    	}
    	
    	
    	getchar ();
    	}
    	
    	int mult (int x, int y)
    	{
    	    return x * y;
    	    }
     
  21. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #21
    Code:
    if (mult (x, y) = 25) 
    
    This is wrong.

    In C, the single = is always assignment. If you want to test for equality, you must use ==.

    Memorization tip: when you see ==, think "equal to". Two ='s; "equal to".


    Second tip: when you have an error, post the complete actual error message (copy and paste it).
     
  22. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #22
    Third tip. Swapping the order in the comparison makes the compiler warn ou that you are trying to assign instead of compare.

    Code:
    if (25 = mult (x, y))
    Will give you a warning.

    B
     

Share This Page