My Blog List

Showing posts with label syntax. Show all posts
Showing posts with label syntax. Show all posts

Friday, June 24, 2011

Method Declarations: Back to Syntax (Fnishing Gestures and Chapter 11)


Hmmmm I got the tutorial working in the iPad Application Development book, chapter 11. But when I got to the section on gestures there was some syntax I'm unsure about. I need to go back to Goldstein's Objective-C book. I worked on that last year, but clearly I need to do some refreshing.

There's developing abilities with the framework but then every once and a while the language trips me up. I can't afford that when I want to focus on the framework documentation. So back-to-grass-tacks for awhile. What are grass tacks anyway? Am I even remembering the idiom correctly?

Going to summarize syntax as I go, starting with chapter 6 of the objective-c book.
  1. Method declarations are like function prototypes in Objective-C (page 146-7)
    1. When there's only one argument, I'll see just one ':' but there are lots of methods that take more than one argument. The number of ":"'s (colons) shows how many argments there are
    2. The code below declares the "createBudget:: method. It has two argments
    3. -(void) createBudget:(double) aBudget 
    4. withExchangeRate: (float) anExchangeRage;
    5. The COLON is considered to be a PART OF THE METHOD NAME
    6. If there are no colons than there are no arguments
  2.  The real method name is createBudget:withExchangeRate 
  3.  Notice that "("'s are used around both the 
    1. return value
    2. argument/data types - because we are casting one type to another (chapter 4)
  4. Inside of the method I am declaring, I will access the arguments using the identifier names 'aBudget' and 'anExchangeRate'

Wednesday, June 15, 2011

Study Notes: Syntax, Picking apart UILabel (sliders)


Study Notes Wednesday, June 15, 2011

Picking apart another line of code to study syntax,  from example on page 236

   sliderDisplay.text = [NSString stringWithFormat:@"%.2f", slider.value);

This is how I’m currently interpreting this line of code.

I’m messaging the NSSTring class to put the value attribute of slider (of type UILabel), a string of a certain format, into the text portion/value of the DISPLAY version of a slider.  I guess they are different because I’ll probably use the sliderDisplay in a view and the slider.value is just where I store things that I may be changing internally. And maybe this has to do with keeping things on the view separate from things I operate and do math on, etc. the value of slider is probably something I change directly. Maybe….

Here are the ideas that I used to figure out my current interpretation.

* Ways to figure out what “.text” means
o   Command option help
o   Check code sense in book
o   Check settings


My sense is that it is an attribute, a property of files of the type/class that sliderDisplay is. Because when I do a ctrl click on it, several classes come up.

So First I will see what class sliderDisplay is in my interface.

IBOutlet    UILabel     *sliderDisplay;

So now when I jump to definition for text, I will look for the one associatd with UILabel




   sliderDisplay.text = [NSString stringWithFormat:@"%.2f", slider.value);

So I think that sliderDisplay is an ivar that can be composed of different pieces/ivars depending on what I need. And I think that .text is one of the pieces/ideas/attributes/properties that I want to address.

I think that I’m saying that I’m picking up a value (the value attribute of the slider object)
    IBOutlet    UISlider    *slider;
    IBOutlet    UILabel     *sliderDisplay;
}

I’m messaging the NSSTring class to put the value attribute of slider (of type UILabel), a string of a certain format, into the text portion/value of the DISPLAY version of a slider.  I guess they are different because I’ll probably use the sliderDisplay in a view and the slider.value is just where I store things that I may be changing internally. And maybe this has to do with keeping things on the view separate from things I operate and do math on, etc. the value of slider is probably something I change directly. Maybe….

·     Notice that ‘text’ and ‘NSString’ are both colored purple.
o    I know that ‘NSString’ is a framework class.
o   What does that tell me about ‘.text’?
o   It’s not a framework class because it doesn’t start with a capital letter
o   It uses dot notation, which I thought could be a method or an ivar.     LOOK IN THE OLDER BOOK FOR DOT NOTATION   
o   Preference settings indicates it could be a CLASS or a TYPE or IVAR or GLOBAL name
o   But I don’t think MY ivars are purple, ‘other’


·     Reencountered the apple documentation on objective-c and programming in objective – c. Good time to look at this again. Plan to start reading through this today after I work a little more on this example.

Tuesday, June 14, 2011

Study Notes: Syntax, Picking Apart the UIColor example

Study Notes: Syntax UIColor example



* Notice there are examples linked from frameworks class documentation. Studying these has been suggested in class and by Goldstein for learning
…..
What the following

   theTextField.textColor    =[UIColor blueColor];

seems to boil down to is

…. Set the textColor attribute (property) of my ‘theTextField’ ivar to be BLUE.


And here are all the details I went through to figure that out
….
I want to better understand this line of code from page 236

    theTextField.textColor =[UIColor blueColor];

“theTextField”

·    is a pointer I defined in the interface file associated with this implementation file.

@interface SettingsViewController : UIViewController {
    <SettingsViewControllerDelegate> delegate;
    float                       sliderValue;
    IBOutlet  UITextField   *theTextField;

·    The pointer is of type UITextField, a frameworks class
·    The part that follows theTextField uses dot notation
o     I THINK textColor is an instance method associated with UITextField



Let’s see if I can figure that out

·     option+ command on UITextField
·     follow link to ()delegate Protocol

NO DICE NO REFERENCE TO textColor


Overview
A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
In addition to its basic text-editing behavior, the UITextField class supports the use of overlay views to display additional information (and provide additional command targets) inside the text field boundaries. You can use custom overlay views to display features such as a bookmarks button or search icon. The UITextField class also provides a built-in button for clearing the current text.
A text field object supports the use of a delegate object to handle editing-related notifications. You can use this delegate to customize the editing behavior of the control and provide guidance for when certain actions should occur. For more information on the methods supported by the delegate, see the UITextFieldDelegate protocol.


Go back up a level looking for textColor (some kind of method I think….)

It is a property of the UITextFieldClass. Don’t quite understand that. I know I use the property declaration to substitute for accesors. Well I least I see it that is something



OK back to

    theTextField.textColor =[UIColor blueColor];

I know that [] syntax means that I’m messaging an object. “Many methods in UIKit require you to specify color data using a UIColor object, and for general color needs it should be your main way of specifying colors. “
 So I’m messaging the UIColor class from the blueColor method.

I guess that I’m saying…. Set the textColor attribute (property) of my ‘theTextField’ ivar to be BLUE.





Understanding UIColor use


·     option+ command on UIColor to get documention

Overview
A UIColor object represents color and sometimes opacity (alpha value). You can use UIColor objects to store color data, and during drawing you can use them to set the current fill and stroke colors.
Many methods in UIKit require you to specify color data using a UIColor object, and for general color needs it should be your main way of specifying colors. The color spaces used by this object are optimized for use on iOS-based devices and are therefore appropriate for most drawing needs. If you prefer to use Core Graphics colors and color spaces instead, however, you may do so.
Most developers should have no need to subclass UIColor. The only time doing so might be necessary is if you require support for additional colorspaces or color models.


·     UIColor is the class
UIColor Class Reference
Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 2.0 and later.
Declared in
UIColor.h
UIInterface.h
·      
·     blueColor is a class method assoc. with UIColor
blueColor
Returns a color object whose RGB values are 0.0, 0.0, and 1.0 and whose alpha value is 1.0.
+ (UIColor *)blueColor
Return Value
The UIColor object.
Availability
·                       Available in iOS 2.0 and later.

Friday, June 3, 2011

Objective-C Online Tutorials

Here are some of the places where I look things up online. There are a number of things I find in the pile of books I have that are NOT in these online tutorials, but these are handy when I'm trying to remember basic syntax.

Reading Notes: Protocol Syntax (<, >) and Initialization




page 166 Goldstein goes through app initialization

I remembered what the < and > are for! They refer to protocol's. Protocols are a new concept for me. I keep thinking of C3PO in Star Wars telling Luke that he's a protocol droid. Which is REALLY not helpful.

A search on the syntax characters plus the word 'protocol' turned up the following in

http://en.wikipedia.org/wiki/Objective-C

- (void)setMyValue:(id)foo;
In the above statement, foo may be of any class.
- (void)setMyValue:(id<aProtocol>)foo;
In the above statement, foo may still be an instance of any class, but the class must conform to the aProtocol protocol.

.....

So when I look at the initialization code for the DeepThoughtsAppDelegate.h file and I see the line

@interface DeepThoughtsAppDelegate:NSObject <UIApplicationDelegate>

that must mean that MY AppDelegate class (DeepThoughtsAppDelegate), that I'm setting up here is 

* A subclass of NSObject AND
* Will conform to the UIApplicationDelegate protocol


Another basic syntax link
http://en.wikibooks.org/wiki/Objective-C_Programming/syntax
...
I NEED TO REMEMBER to double check on the use of "@class", which is not in the current book. 
I THINK it just means that I can use the DeepThoughtsViewController class from here

//  DeepThoughtsAppDelegate.h
//  DeepThoughts
//
//  Created by xxxxx on 5/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@class DeepThoughtsViewController;

@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    DeepThoughtsViewController *viewController;
}
Other things I'm interpreting this code as doing is
declaring two instance variables window and viewController that will be part of the DeepThoughtsAppDelegate class. The window ivar is of type UIWindow and the viewController ivar inheirits from DeepThoughtsViewController (probably why I need to put that @class statement in)
* * *
A little less mystery about the code now



Thursday, June 2, 2011

Reviewing Syntax in framework generated code

Another place to review this is chapter 9 , page 247 in Stevenson's Cocoa and Objective-C, Up and Running
 
Interpreting framework generated code

* application didFinishLaunchingWithOptions is one of the key methods in the MVC design pattern. I thinnnnnkkk it’s the Controller part

* application is a method, it’s passing a parm an object named UIApplication 

* NSDictionary is a paramater being passed to the method application didFinishLaunchingWithOptions

* It returns a Boolean value

I don’t quite recall why/how the signifigence of packing all the method calls together, but I remember that there’s something similar done in C++, so look back there

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  


The “window” object receives the “addSubview” message

    [self.window addSubview:viewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}


Searched in help on “addSubView”

Ended up in UIView class reference

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabel object draws a text string and a UIImageView object draws an image.
….
Managing the View Hierarchy
      superview  property
      subviews  property
      window  property
        – addSubview:

addSubview:
Adds a view to the end of the receiver’s list of subviews.
- (void)addSubview:(UIView *)view
Parameters
view
The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.
Discussion
This method retains view and sets its next responder to the receiver, which is its new superview.
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

In the viewController.m generated code, the class is declared.

The class inherits from NSObject

@class DeepThoughtsViewController;

@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    DeepThoughtsViewController *viewController;
}


·   create a pointer to an object of type UIWindow names “window”
·   create a pointer to an object “viewController” with a data type “DeepThoughtsViewController”

Next question to remember the answer to (I know I was able to interpret this just a couple of weeks back!)

-    The role of the < and >
- I remember that the interface is where we give the preview for the methods
@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {