-1

Here I brought some of the doubt when I used to learn Objective-C and I have searched Google not able to get the perfect answer. I did this from one blog for beginners for Objective-C.

First I will show my code:

@implementation ViewController

@synthesize textfield 1;
@synthesize textfield 2;
@synthesize textfield 3;
@synthesize totalTextField;

- (void)viewDidLoad {
    [super viewDidLoad];
//    // Do any additional setup after loading the view.
    self.navigationItem.title = @“calculate the 3 field“;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)calculate:(id)sender {
    int firstNumber = [[self.textfield 1 text] intValue];
    int secondNumber = [[self.textfield 2 text] intValue];
    int thirdNumber = [[self.textfield 3 text] intValue];

    //NSLog(@"firstNumber: %i , secondNumber: %i, thirdNumber: %i", firstNumber, secondNumber, thirdNumber);
    float total = (CGFloat) firstNumber/2 + secondNumber/2 + thirdNumber/2;
    NSLog(@“total is: %f", total);
    NSString *mark = [NSString stringWithFormat:@"%.2f", total];
    self.totalTextField.text = mark;
}

Explaniation:

I have three text field called textfield 1,textfield 2,textfield 3. When the user enters a number in the three text fields they have to press the calculate button. After that some calculation will go and display the final result in totalTextField.

My questions:

  1. Why in Xcode the default files called Appdelegate, view controller.? What is the actual meaning for that two? Why they are default and what will happen if we remove Appdelegate.h .m files? What is the meaning for viewcontroller, Appdelegate?

  2. In my above code they use inside calculate button method:

    int firstNumber = [[self.textfield 1 text] intValue];
    

    why they din't use:

    NSInteger * soemname = // alloc init //
    
  3. And also they used float. Why can't use CGFloat?

Can any one tell me why they used int instead of NSInteger and all? And if it is possible can any one recode that int with NSInteger to work as same?

Please help me to clear this doubt. I am newbie in learning process.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
stepan ferous
  • 37
  • 1
  • 7

1 Answers1

0

1. AppDelegate is the default implementation of a delegate class for UIApplication. For details on what the delegate is for, see the documentation for UIApplication and UIApplicationDelegate protocol. ViewController is the name given to a UIViewController subclass that several of the templates create for you when you create a new project. View Controllers are the classes that manage an app's UI.

2. NSInteger is an int that changes bit-width, depending on platform. For iOS, this means that it's either 32 bits wide (older hardware) or 64 (newer hardware). int is always 32-bits wide on iOS. The reason to use one over the other depends on the scenario. Sometimes it's better to have a known width that won't change. Sometimes it's better to go with the "native" integer size. There's no hard and fast rule for picking one over the other.

3. float is the basic C type. CGFloat is platform-dependent, and the primitive type may change (float, double). There's little reason to use CGFloat unless one is interacting with Core Graphics in some way.

Avi
  • 7,469
  • 2
  • 21
  • 22