Starting Objective-C

Discussion in 'iOS Development' started by gojohnnyboi, Oct 8, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    For you dudes who have been spamming it up for ages, I'm gonna tell you in my opinion what to do here.

    First you must know that you cannot learn to program over night. You cannot even(most likely) learn the structure, jobs, and uses of the compilation tools you are handed with developer applications/platforms. As a start, here is what i will provide to you.

    You will need:

    -A Compiler (Either XCode's or another. What this does is take the code, and convert it to a useable format for your system's assembly)

    - An Assembler (This basically creates the base, raw binary, unlinked, and unusable from your code)

    - A Linker (This "connects" your binary to the libraries and frameworks on your system*or the system you're cross-compiling for* so that it may run properly, and use the APIs within those libraries)

    Your Headers (These are "guidelines" for the compiler. They tell it what can be used in your code, and how you may use them. Failure to use them in such a manner will result in compilation errors)


    Now, objective-c, much like c++, derives from C. To cross-compile for iPhoenOS, you will be using the GNU Compiler Collection (gcc), which is what xcode uses, as well as the toolchain(they're the same compiler, just apple built one =p). You may read up on that as well as the objective-c language at http://gcc.gnu.org . Now, for the frameworks available for iPhoneOS, and documentation/sample code on using them, you can visit the iPhone Developer Center at http://developer.apple.com/iphone .


    Although you *can* start straight off with objective-c, i would recommend(at least for a couple weeks), go and study hard on how gcc works, and maybe start learning with JavaScript, or an alternative language that is easier than what you're doing here. Many people who just jump right off on coding, without reading up first, and just copy and paste sample code simply do not know what they're doing in the end. They 1. copy 2. paste 3. guess and check if the code works or not.


    Another thing i cannot stress enough is the memory management in objective-c. Thankfully, we are lucky enough to have an EXPERT on this here on the forums, so please see SkylarEC's thread on memory management/leaks at
    http://www.ifans.com/forums/showthread.php?t=93567

    That will provide you with a good start on understanding how the system memory works. In objective-c, you will mostly(unless you do Objc++), work with objective-c classes. all UIKit(apple iphone's user interface framework, used by EVERY application*with exception to command line apps*) objects are objective-c classes. you will be creating instances of them, and telling them what to do. you also will be creating subclasses, or your own classes based on a superclass of an object. In objective-c, you create your object, and then send messages to it which tells it what to do. for example.

    Code:
    UILabel* myLabel = [[UILabel alloc] initWithFrame: someFrame];
    [myLabel setText:@"BLAH"];
    when i created the UILabel(part of UIKit, again) i initialized it with "someFrame", which i will have to define somewhere else in my code. then, i use UILabel's setText: method to set what string of text it will display.

    Check out those links, and feel free to post question here, or PM me(within reason! i'm still a human!). I'm sure another developer or myself will be happy to answer your question.

    -John
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    hey gojohhnyboi I PM'd you have you seen yet I do need help!
  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Meh, you may have sent it when my inbox was full. please post it again here, and i'll try my best
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    ok I may have fixed it by getting SDK headers I'll post again if it dosen't work I will try now
    ------------------double post merged------------------
    Code:
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:reasonableFrameForButton];
    [button setImage:[UIImage imageNamed:@"RedButtonImage.png" ForState:UIControlStateNormal];
    [self.view addSubview:button];
    header file
    Code:
    /* Import our headers */
    
    #import <UIKit/UIHardware.h>
    #import <Foundation/Foundation.h>
    #import <CoreFoundation/CoreFoundation.h>
    #import <UIKit/UIKit.h>
    #import <CoreGraphics/CoreGraphics.h>
    #import <stdio.h>
    #import <stdlib.h>
    #import <time.h>
    #import <GraphicsServices/GraphicsServices.h>
    #import <WebCore/WebFontCache.h>
    #import <sys/reboot.h>
    #import <notify.h>
    
    
    
    /* Define what objects and selectors will be in the application  class */
    
    @interface myapp : UIApplication
    {
    	
    	UIWindow* window;
    	UIView* mainView;
    	CGRect rect;
    	UINavigationBar* navBar;
    	UIAlertSheet* sheet;
    	UIButton* button;
    	
    }
    
    -(void)applicationDidFinishLaunching: (id) unused;
    -(void)dealloc;
    
    /* Always end the class with @end */
    
    @end
    	
    gives errors of

    Code:
    error: 'button' undeclared (first use in this function)
    error: (Each undeclared identifier is reported only once
    error: for each function it appears in.)
    error: 'UIButton' undeclared (first use in this function)
    error: 'UIButtonTypeCustom' undeclared (first use in this function)
    error: 'UIControlStateNormal' undeclared (first use in this function)
    error: incompatible type for argument 1 of 'setFrame:'
    please help me
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    you must import the header, so that the compiler knows what it is. otherwise you're trying to pull it out of thin air.

    also, since you created it in your header, you do not need to RECREATE it again.

    Code:
    button =
    Code:
    #import <UIKit/UIButton.h>
    also, create your own frame and set it

    Code:
    [button setFrame:CGRectMake(20.0f, 100.0f, 280.0f, 50.0f)];
  6. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    when i include it in my header it says there is no such directory
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Those aren't selectors, they're method prototypes. A selector is a variable.
    You don't need to include or import any UIKit headers, they should all be imported when you import <UIKit/UIKit.h> (take a look at the header) [EDIT: That same is true with all the frameworks]. Finally, if you have an object, but not the header, you can tell the compiler that it is a forward class. That is, that it is indeed a class, but it will be defined later, in the frameworks in this case, by adding @class UIButton; in your interface. If you compiler throws you errors about your forward class' method, just use an extern or two, and all will be well.
  8. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Then you obviously do not have all of the UIKit headers. May i suggest using class-dump to obtain them.

    You can do this also.
  9. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I got some i'll put them in the folder when I get back to my computer and post the errors
    ------------------double post merged------------------
    i get all this sh*t when i include it

    Code:
    n file included from iBoot.h:14,
                     from iBoot.m:3:
    /usr/local/include/UIKit/UIButton.h:10:29: error: UIKit/UIGeometry.h: No such file or directory
    /usr/local/include/UIKit/UIButton.h:11:34: error: UIKit/UIStringDrawing.h: No such file or directory
    /usr/local/include/UIKit/UIButton.h:12:31: error: UIKit/UIKitDefines.h: No such file or directory
    In file included from iBoot.h:14,
                     from iBoot.m:3:
    /usr/local/include/UIKit/UIButton.h:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'interface'
    /usr/local/include/UIKit/UIButton.h:30: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:31: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:32: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:34: error: expected specifier-qualifier-list before 'UILineBreakMode'
    /usr/local/include/UIKit/UIButton.h:52: error: expected specifier-qualifier-list before 'UILineBreakMode'
    /usr/local/include/UIKit/UIButton.h:53: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:54: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:57: error: expected specifier-qualifier-list before 'UIEdgeInsets'
    /usr/local/include/UIKit/UIButton.h:68: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:69: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:70: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:71: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:72: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:74: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:75: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:76: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:77: error: expected ')' before 'UIControlState'
    /usr/local/include/UIKit/UIButton.h:78: error: expected ')' before 'UIControlState'
    iBoot.m: In function '-[iBoot applicationDidFinishLaunching:]':
    iBoot.m:35: error: incompatible type for argument 1 of 'setFrame:'
    iBoot.m:36: error: 'UIControlStateNormal' undeclared (first use in this function)
    iBoot.m:36: error: (Each undeclared identifier is reported only once
    iBoot.m:36: error: for each function it appears in.)
  10. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Ouch! You're not using iPhone-GCC are you? If so, it's really gonna be a pain in the ass to mod all those headers just right. Build the toolchain. http://saurik.com/id/4

Share This Page