objective c - Invalid Summary in NSString assignment -
i trying implement simple calculator have update current operation (+, -, * etc) in variable of type nsstring. current operation received model parameter.
-(double)dooperation:(nsstring *)operation withoperand:(double)operand { if (!currentoperation) { anoperand = operand; } currentoperation = operation; //invalid summary in currentoperation; + in operation }
what wrong in this? if direct assignment of pointers not allowed nsstring, alternate method?
edit
- to more precise, legal in ios assign nsstring = sign?
- if not, way go.
note: currentoperation private variable in controller class , operation parameter method.
here complete code
@interface calculatorbrain : nsobject { double anoperand; nsstring * currentoperaion; } - (double) dooperation: (nsstring *) operation withoperand: (double) operand; @end @implementation calculatorbrain - (double) dooperation: (nsstring *) operation withoperand: (double) operand { if (!currentoperaion) { anoperand = operand; } else if([currentoperaion isequal: @"+"]) { anoperand += operand; } else if([currentoperaion isequal: @"-"]) { anoperand -= operand; } else if([currentoperaion isequal: @"*"]) { anoperand *= operand; } else if([currentoperaion isequal: @"/"]) { anoperand /= operand; } currentoperaion = operation; //this statement doesn't work expected if ([currentoperaion isequal: @"="]) { currentoperaion = nil; } return anoperand; } @end
to more precise, legal in ios assign nsstring = sign?
yes.
what wrong in this?
you not taking ownership of string lead problems if want use instance later, see memory management guide:
you can take ownership of object using
retain
.
remember object may have more 1 owner. taking ownership of object way of saying need kept alive.
you -retain
string, or -copy
avoid mutation external code, e.g.:
// in -dooperation:: [currentoperation release]; currentoperation = [operation copy]; // copy if operation might mutable // relinquish ownership: - (void)dealloc { [currentoperation release]; [super dealloc]; }
also declared properties.
Comments
Post a Comment