Objective-C, iPhone SDK basics - iphone

OK, this is going to be a stupid question but anyway I have nowhere to ask it except here.
I have two buttons and there must be a switch-case statement performed on tapping any of them.
Of course I can put this statement in each IBAction code block but this code would look terribly.
I tried to put swith-case into a separate method and this is what I have:
.h file
#import <UIKit/UIKit.h>
#interface AppViewController : UIViewController
{
IBOutlet UILabel *someTextLabel;
NSNumber *current;
}
#property (nonatomic, retain) IBOutlet UILabel *someTextLabel;
#property (nonatomic, retain) NSNumber *current;
- (void) switchMethod;
- (IBAction) pressButtonForward;
- (IBAction) pressButtonBack;
#end
.m file
#import "AppViewController.h"
#implementation AppViewController
#synthesize someTextLabel;
#synthesize current;
current = 0;
- (void) switchMethod:current
{
switch((int)current) {
case 0:
//do something
break;
case 1 :
//do something
break;
//etc
default:
//do something
break;
}
}
- (IBAction) pressButtonBack
{
if((int)current == 0) {
current = 6;
}
else {
current--;
}
//here must be a switchMethod performed
}
- (IBAction) pressButtonForward
{
if((int)current == 6) {
current = 0;
}
else {
current++;
}
//here must be a switchMethod performed
}
//auto-generated code here
#end
Of course this code is incorrect but this is just like a blueprint of what I wanted to get.
Questions:
What is a correct way of using such switch-case statement as a separate method so that I could call it from IBAction methods?
How should I cast data types for this code to work, or would it be better to use integer type (for "current" variable) everywhere?

I haven't really got your question but maybe you should switch between forward and backward cases using a method like this :
- (IBAction)pressButton:(id) sender;
and depending on the value of sender, you can maybe switch case between Forward And Backward.
Thus, your code will be maybe more readable and you won't have to duplicate your switch.
N.B. : If your only problem is the duplication of your switch and if you don't want to use my method, I don't understand why you don't call directly your switchMethod in the two functions...

Answer to question 1:
just send a message to a self to invoke the switch method.
Answer to question 2:
NSNumber is an object that wraps a numeric value. To convert an NSNumber to an int:
int myIntValue = [number intValue];
To convert an int to a NSNumber
NSNumber* number = [NSNumber numberWithInt: myIntValue];
However, in your example, it's better to just define current as an int. So your code should look something like this:
#interface AppViewController : UIViewController
{
IBOutlet UILabel *someTextLabel;
unsigned int current;
}
#property (nonatomic, retain) IBOutlet UILabel *someTextLabel;
#property (nonatomic, assign) unsigned int current; // nonatomic might be redundant for POD types
- (void) switchMethod;
- (IBAction) pressButtonForward;
- (IBAction) pressButtonBack;
#end
#implementation AppViewController
#synthesize someTextLabel;
#synthesize current;
//current = 0; this wouldn't compile, in any case it is redundant, ivars start out as 0.
- (void) switchMethod
{
switch(current)
{
// do switchy stuff
}
}
- (IBAction) pressButtonBack
{
if(current == 0)
{
current = 6;
}
else
{
current--;
}
[self switchMethod];
}
- (IBAction) pressButtonForward
{
if(current == 6)
{
current = 0;
}
else
{
current++;
}
[self switchMethod];
}
// etc
#end
NB although I defined a property for current, I haven't used it in the code, which is a bit of a no-no. The main problem is that if anybody decides to observe current using KVO, they won't be notified of the changes. I should really have written things like
switch([self current])...
and
[self setCurrent: 6]...
etc

Related

Instance class or method without an interface?

Got this code:
#import <Foundation/Foundation.h>
#interface CalculatorBrain : NSObject
- (void)pushOperand:(double)operand;
- (double)performOperation:(NSString *)op;
#property (nonatomic, readonly) id program;
+ (NSString *)descriptionOfProgram:(id)program;
+ (double)runProgram:(id)program;
#end
And this one:
#import "CalculatorBrain.h"
#interface CalculatorBrain()
#property (nonatomic, strong) NSMutableArray *programStack;
#end
#implementation CalculatorBrain
#synthesize programStack = _programStack;
- (NSMutableArray *)programStack
{
if (_programStack == nil) _programStack = [[NSMutableArray alloc] init];
return _programStack;
}
- (id)program
{
return [self.programStack copy];
}
+ (NSString *)descriptionOfProgram:(id)program
{
return #"blablabla";
}
- (void)pushOperand:(double)operand
{
[self.programStack addObject:[NSNumber numberWithDouble:operand]];
}
- (double)performOperation:(NSString *)operation
{
[self.programStack addObject:operation];
return [[self class] runProgram:self.program];
}
+ (double)popOperandOffProgramStack:(NSMutableArray *)stack
{
double result = 0;
return result;
}
+ (double)runProgram:(id)program
{
NSMutableArray *stack;
if ([program isKindOfClass:[NSArray class]]) {
stack = [program mutableCopy];
}
return [self popOperandOffProgramStack:stack];
}
#end
The code is fine an it runs, so the question is, Where is declared popOperandOffProgramStack in the interface? why it compiles and it's okay? it should crash but I can not find an explanation to this....
Thank you!
You only need to declare methods in the #interface in the .h file if you're exposing them to the world. Otherwise, no declaration needed.
And nowadays, the order that they appear in the implementation doesn't matter, either. Historically, if the method was implemented later in the #implementation than where it was invoked, you needed to have the method declared above (generally in the #interface). Now the compiler doesn't care whether the implementation is earlier or later in the .m file.
the compiler can sees its definition:
+ (double)popOperandOffProgramStack:(NSMutableArray *)stack
{
double result = 0;
return result;
}
so it is able to confirm it has been declared, the parameter types, and return type.
also - in older compilers, it would need to precede usage, but not anymore if used in the #implementation scope.
even if it were not declared, objc is weak enough that it would not be a compiler error (warning, perhaps). exception: the method must be visible if you're using ARC. the compiler needs to know the reference counting semantics and parameter types when ARC is enabled.

Alternative / Similar to tag in UIView

Is there any alternative to use like tag to property of UIView? The thing is I'd like to pass UITextField some NSInteger. Way to do is tag. But I want to pass 2 different NSInteger.
Any ideas?
You could subclass UITextField and add two NSInteger properties to the class.
#interface CustomTextField : UITextField
#property (nonatomic, assign) NSInteger x;
#property (nonatomic, assign) NSInteger y;
#end
You can attach any data to any object using an Associative Reference. This is a very handy approach. It even correctly handles memory management. I sometimes use a category to wrap these so I can create new properties on an existing class. For example, in one project I'd like every view controller to know about a special label (like how they all know about navigationController). I do it this way:
#interface UIViewController (MYSpecialViewController)
#property (nonatomic, readwrite, strong) UILabel *specialLabel;
#end
#implementation UIViewController (MYSpecialViewController)
static const char kMySpecialLabelKey;
- (UILabel *)specialLabel
{
return objc_getAssociatedObject(self, &kMySpecialLabelKey);
}
- (void)setSpecialabel:(UILabel *)value
{
objc_setAssociatedObject(self, &kMySpecialLabelKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end
You can find a simple working example using a UIAlertView in the iOS5:PTL sample code for Chapter 3.
Take the general, object-oriented solution, but remember you can a stuff lot of data into a tag:
- (uint32_t)pack:(uint16_t)a with:(uint16_t)b {
return (uint32_t)(a << 16 | b);
}
- (uint16_t)getA:(uint32_t)pair {
return (uint16_t)((pair & 0xffff0000) >> 16);
}
- (uint16_t)getB:(uint32_t)pair {
return (uint16_t)(pair & 0xffff);
}
// use it
- (void)setupSomeView {
someView.tag = [self pack:1024 with:2048];
}
- (IBAction)someControlEventHappened:(id)sender {
NSLog(#"%d %d", [self getA:sender.tag], [self getB:sender.tag]);
}
Caveats:
#RobNapier's answer is more general, more correct. The only
advantage this way is that it's quick and dirty
Works for pairs of unsigned ints < 32k
Works NSInteger implementations >= 32 bits, which is everywhere, I think.

Storing and opening data in a variable

I'm making an app that calculates certain things.
I need it to be able to take the input from the first textfields, for example 4+4 and save the result in a variable.
In the second text fields there could be 8+8 for example, and the result of that will also be saved into a variable (possibly the same).
Third row of textfields could yield more numbers etc, etc..
In the end there will be a button "Calculate" for example. And that will take the results from first, second, third etc textfields and calculate all of those together and output the end result.
The calculations are of course more advanced than this, but I just need the basic/simple idea of how to do this.
There is no need for saving the data to a file just now, it should just be in the app while the other textfields are being filled.
For 0x8badf00d:
Header.
#interface UnitConverterViewController : UIViewController {
NSMutableArray *calculationsArray;
UITextField *m1Text;
UITextField *m2Text;
}
#property (nonatomic, retain) IBOutlet UITextField *m1Text;
#property (nonatomic, retain) IBOutlet UITextField *m2Text;
#property (nonatomic, retain) IBOutlet NSMutableArray *calculationsArray;
#end
Implementation:
#implementation UnitConverterViewController
#synthesize m1Text, m2Text, calculationsArray;
#synthesize resultTotal = _resultTotal;
-(id)init {
if(self = [super init]){
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute{
NSString* sumString = [NSString stringWithFormat:#"%d",[m1Text.text intValue]+[m2Text.text intValue]];
[calculationsArray addObject:sumString];
}
-(IBAction)calculate{
int total=0;
for(NSString* sumStr in calculationsArray){
total = total+[sumStr intValue];
}
NSLog(#"Total: %d", total);
[calculationsArray release], calculationsArray = nil;
}
I must be doing something wrong, and I know I need a way to output this, a label and such. But for now I need to know if what I've done so far is correct, and what the best way of outputting it would be.
You should declare the variables to store the results in your header file, these are than accessible from anywhere in your .m file, the same goes for your text fields.
For example:
Calculator.h
#interface Calculator: SuperclassName{
UITextField *_fldOne;
UITextField *_fldTwo;
UITextField *_fldThree;
UITextField *_fldFour;
int resultOne;
int resultTwo;
int _resultTotal;
}
#property(nonatomic, readonly) int resultTotal;
- (void) calculate;
#end
Calculator.m
#implementation Calculator
#synthesize resultTotal = _resultTotal;
- (void) calculate{
resultOne = [_fldOne.text intValue] * [_fldTwo.text intValue];
resultTwo = [_fldThree.text intValue] / [_fldFour.text intValue];
totalResult = resultOne + resultTwo;
}
#end
In this example resultOne and Two, and all the textfields are available throughout your class to work with, the totalResult is set as a readonly property and synthesized to create a getter automaticaly (which returns the value stored in _totalResult because of synchronizing like totalResult = _totalResult) as so it is available to read from outside the class.
As long as it all happens on one screen it should be more than enough, but of course you could make an NSDictionary or NSArray but that seems unnecessary here.
Hope this helps
Save the result to array. Lets say you have NSMutableArray* calculationsArray;//iVar
//initialize calculationsArray in init method
-(id)init
{
if(self = [super init])
{
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute
{
NSString* sumString = [NSString stringWithFormat:#"%d",[textField1.text intValue]+[textField2.text intValue]);
[calculationsArray addObject:sumString];
}
- (IBAction)calculate
{
int total=0;
for(NSString* sumStr in calculationsArray)
{
total = total+[sumStr intValue];
}
NSLog(#"Total: %d",total);
[calculationsArray release],calculationsArray = nil;
}

How to hide variables for distributed code

So I've built a few apps and am now trying my hand at building a piece of iPhone code that others can drop into their applications. Question is how do I hide the data elements in an object class header file (.h) from the user?
For example, not sure if people have used the medialets iPhone analytics but their .h does not have any data elements defined. It looks like:
#import <UIKit/UIKit.h>
#class CLLocationManager;
#class CLLocation;
#interface FlurryAPI : NSObject {
}
//miscellaneous function calls
#end
With that header file, they also supply an assembly file (.a) that has some data elements in it. How do they maintain those data elements across the life span of the object without declaring them in the .h file?
I am not sure if it matters but the .h file is only used to create a singleton object, not multiple objects of the same class (FlurryAPI).
Any help would be much appreciated. Thanks
Take a look at this:
Hide instance variable from header file in Objective C
In my header file I'd have:
#interface PublicClass : NSObject
{
}
- (void)theInt;
#end
In my source file I'd have:
#interface PrivateClass : PublicClass
{
int theInt;
}
- (id)initPrivate;
#end;
#implementation PublicClass
- (int)theInt
{
return 0; // this won't get called
}
- (id)init
{
[self release];
self = [[PrivateClass alloc] initPrivate];
return self;
}
- (id)initPrivate
{
if ((self = [super init]))
{
}
return self;
}
#end
#implementation PrivateClass
- (int)theInt
{
return theInt; // this will get called
}
- (id)initPrivate
{
if ((self = [super initPrivate]))
{
theInt = 666;
}
return self;
}
#end
I'm using theInt as an example. Add other variables to suit your taste.
I recommend you to use categories to hide methods.
.h
#import <Foundation/Foundation.h>
#interface EncapsulationObject : NSObject {
#private
int value;
NSNumber *num;
}
- (void)display;
#end
.m
#import "EncapsulationObject.h"
#interface EncapsulationObject()
#property (nonatomic) int value;
#property (nonatomic, retain) NSNumber *num;
#end
#implementation EncapsulationObject
#synthesize value;
#synthesize num;
- (id)init {
if ((self == [super init])) {
value = 0;
num = [[NSNumber alloc] initWithInt:10];
}
return self;
}
- (void)display {
NSLog(#"%d, %#", value, num);
}
- (void)dealloc {
[num release];
[super dealloc];
}
#end
You can't access to the private instance variables via dot notation, but you can still get the value by using [anObject num], though the compiler will generate a warning. This is why our apps can get rejected by Apple by calling PRIVATE APIs.

iPhone: initialize object in controller

I am very new to objective-c and having a problem to initialize an object in view controller. The problem I am having is that when setTemp method is called, "0" is printed on the screen instead of the value of cTemp I would like it to be. Can anyone help me on this problem?
Below are excerpts of the code I have.
SJT.h
#import <Foundation/Foundation.h>
#import <stdlib.h>
#interface SJT : NSObject {
int cTemp;
}
- (int) newTemp;
#end
SJT.m
#import "SJT.h"
#implementation SJT
- (int) newTemp
{
cTemp = 25 + rand() % 8;
return cTemp;
}
#end
SJTViewController.h
#import <UIKit/UIKit.h>
#class SJT;
#interface SJTViewController : UIViewController {
IBOutlet UILabel *temp;
SJT *sjt;
}
#property (retain, nonatomic) UILabel *temp;
#property (retain, nonatomic) SJT *sjt;
- (IBAction) setTemp: (id) sender;
#end
SJTViewController.m
#import "SJTViewController.h"
#import "SJT.h"
#implementation SJTViewController
#synthesize temp;
#synthesize sjt;
- (IBAction) setTemp: (id) sender
{
NSString *tempText = [[NSString alloc] initWithFormat:#"%d",sjt.newTemp];
temp.text = tempText;
[tempText release];
}
.
.
.
#end
The problem is that you're mistaking property syntax for a method call; i.e.
sjt.newTemp
would become a call to [sjt newTemp]. Which happens to be exactly what you want, except that you have not specified in your header/implementation that there actually is a property called newTemp.
So, in this scenario what you want to do is either a) define the property in the header:
#property(nonatomic, readonly) int newTemp;
or b), just call the method newTemp:
[sjt newTemp]
Are you certain that sjt is not nil? You don't provide the code where and instance of SJT is constructed. In Objective-C you can call a method on a nil reference without error, and if you do so on a method that returns an int it will return 0.
So sjt.newTemp will return 0 if sjt is nil.
Both Jacob and teabot have pointed out valid possible reasons -- which one is correct (or both!) depends on pieces of code we can't see in your post.
Based on what you've written so far, you might not be thinking of newTemp as a property, but more as a function call, so I would suggest changing your code to:
- (IBAction) setTemp: (id) sender {
int tempInt = [self.sjt newTemp];
self.temp.text = [NSString stringWithFormat:#"%d", tempInt];
}
which is functionally equivalent. Note the convenience constructor stringWithFormat: returns an autoreleased object, which is then retained by the retain property text of the temp UILabel.
The other thing to double-check in your code is that self.sjt is not nil, which is exactly what teabot said. Objective-C returns 0 on method calls invoked on a nil pointer.