How to compare data in iPhone sdk - iphone

I am using the condition,but it don't go to the if condition, every time goes to the else condition.
if ([[dict2 valueForKey:#"meeting_location"] isEqual:#""])
{
lbllocation.text = #"-----";
}
else
{
lbllocation.text = [NSString stringWithFormat:#"Location: %#",[dict2 valueForKey:#"meeting_location"]];
}
Output came from Webservice:-
"meeting_location" = "";
"meeting_time" = "";
"meeting_with" = "";

Use Below Code -
if ([[dict2 valueForKey:#"meeting_location"] length] == 0)
{
lbllocation.text = #"-----";
}
else
{
lbllocation.text = [NSString stringWithFormat:#"Location: %#",[dict2 valueForKey:#"meeting_location"]];
}
Hopefully this will work here.

Related

Get incoming number from a Linphone call

I am trying to detect the number that is calling me during a Linphone call. I have tried
case LinphoneCallConnected:
NSLog("callStateChanged: LinphoneCallConnected")
NSLog("CALL ID: \(linphone_call_log_get_call_id(linphone_call_get_call_log(linphone_core_get_current_call(lc)))!)")
but that is null. Is there another way?
in my app I take linphoneCore and linphoneCall and after call linphone_call_get_remote_address. Now you have linphoneAddress from which you can extract username linphone_address_get_username.
Full code here:
- (NSString *)userNameFromCurrentCall {
LinphoneCore *lc = [LinphoneManager getLc];
LinphoneCall *currentcall = linphone_core_get_current_call(lc);
if (currentcall != NULL) {
LinphoneAddress const * addr = linphone_call_get_remote_address(currentcall);
if (addr != NULL) {
return [NSString stringWithUTF8String:linphone_address_get_username(addr)];
}
}
return nil;
}
SString *)userNameFromCurrentCall {
LinphoneCore *lc = [LinphoneManager getLc];
LinphoneCall *currentcall = linphone_core_get_current_call(lc);
if (currentcall != NULL) {
LinphoneAddress const * addr = linphone_call_get_remote_address(currentcall);
if (addr != NULL) {
return [NSString stringWithUTF8String:linphone_address_get_username(addr)];
}
}
return nil;

Expected Expression iOS Xcode

I'm trying to make a simple Calculator App and I'm following instructions in this video ( https://www.youtube.com/watch?v=Uzq41DRC0-Q )
However, when i got to about 9:14, I got an error saying "Expected Expression" and some mark before the else statement. I cannot post images now, so post me everything you know, please. I'm a newbie, so any help would be appreciated!
My code (ViewController.m) :
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)ClearPressed:(id)sender {
}
- (IBAction)MinusPressed:(id)sender {
}
- (IBAction)PlusPressed:(id)sender {
}
- (IBAction)EqualsPressed:(id)sender {
}
- (IBAction)NumberPressed:(UIButton*)sender;{
NSInteger tag = sender.tag;
if (operatorPressed == FALSE) {
if (firstEntry == NULL) {
firstEntry = [NSString stringWithFormat:#"%li", (long)tag];
_OutputLabel.text = firstEntry;
}
}
else {
firstEntry = [NSString stringWithFormat:#"%#%li",firstEntry ,(long)tag];
_OutputLabel.text = firstEntry;
}
else { //HERE MY PROBLEM IS
if (secondEntry == NULL) {
secondEntry = [NSString stringWithFormat:#"%li", (long)tag];
_OutputLabel.text = secondEntry;
}
else {
if secondEntry = [NSString stringWithFormat:#"%#%li",firstEntry ,(long)tag];
_OutputLabel.text = secondEntry;
}
}
}
#end
You have two else statements. That's a no no.
- (IBAction)NumberPressed:(UIButton*)sender;{
NSInteger tag = sender.tag;
if (operatorPressed == FALSE) {
if (firstEntry == NULL) {
firstEntry = [NSString stringWithFormat:#"%li", (long)tag];
_OutputLabel.text = firstEntry;
}
}
else { // 1
firstEntry = [NSString stringWithFormat:#"%#%li",firstEntry ,(long)tag];
_OutputLabel.text = firstEntry;
}
else { // 2
if (secondEntry == NULL) {
secondEntry = [NSString stringWithFormat:#"%li", (long)tag];
_OutputLabel.text = secondEntry;
}
else {
if secondEntry = [NSString stringWithFormat:#"%#%li",firstEntry ,(long)tag];
_OutputLabel.text = secondEntry;
}
}
}
Check where I marked off with 1 and 2. The first else should be else if (someCondition). You can't have two else statements.

Multidimensional array issues

I am creating a multidimensional array for sections / rows based on json data from our API. Looking at the logs the adding rows and sections look good but when I nslog the sections it only shows a bunch of the last object. It seems like the rows are not being added to sections.
What am I doing wrong to make the last API object show in the sections x times? x represents the json count. self.appointments is an NSArray *
According to objective-c multi-dimensional array it should work.
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [[NSMutableArray alloc] init];
NSMutableArray *rows = [[NSMutableArray alloc] init];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
NSLog(#"Dates: %# - %#", date, lastDate);
if (date != lastDate) {
if (lastDate == nil) {
NSLog(#"Adding Row 1");
[rows addObject:dict];
lastDate = date;
} else {
NSLog(#"Adding Section 1");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
[rows removeAllObjects];
NSLog(#"Adding Row 2 %#",[dict objectForKey:#"start_time"]);
[rows addObject:dict];
lastDate = date;
}
} else {
NSLog(#"Adding Row 3");
[rows addObject:dict];
}
}
if (rows) {
NSLog(#"Adding Section 2");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
}
NSLog(#"Sections: %#", sections);
self.appointments = [sections mutableCopy]; //I have also tried self.appointments = sections
sections = nil;
rows = nil;
Logs show
Sections: (
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
),
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
), ... over and over again.
With:
[sections addObject:rows];
[rows removeAllObjects];
[rows addObject:dict];
you keep adding the same rows object not a new one and keep changing the contents so the last contents display each time.
Try:
[sections addObject:[rows mutableCopy]];
[rows removeAllObjects];
[rows addObject:dict];
That way a new row is added each time.
Perhaps better yet:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [NSMutableArray array];
NSMutableArray *rows = [NSMutableArray array];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
if ([date isEqualToString:lastDate] == NO) {
if (lastDate == nil) {
[rows addObject:dict];
lastDate = date;
} else {
[sections addObject:rows];
rows = [NSMutableArray array];
[rows addObject:dict];
lastDate = date;
}
[rows addObject:dict];
}
}
if (rows) {
[sections addObject:rows];
}
self.appointments = sections;
Note that assigning 0 to an object does not release it, release does. But if you use convenience methods to create objects then they are autoreleased and no further action is necessary to release them.
Also note that when comparing strings one needs to use the isEqualToString: method to compare the contents, just using the = or != only compares the address of the strings.

Obj C code's not working, isn't evaluating a method properly?

I'm trying to make an app to convert a decimal number into a binary number, but when I input the decimal number into a text field and have a button call the binaryConvert method, it continually returns 111111111111111111 (1 for every evaluation). Why is this??
- (NSString*)binaryConvert:(int)decNum {
int i = 1;
int value = 524288;
NSString * binary = #".";
while (i <= 19) {
if ((decNum/value) >= 1) {
binary = [binary stringByAppendingString:#"1"];
decNum -= value;
} else {
binary = [binary stringByAppendingString:#"0"];
}
value /= 2;
i++;
}
return binary;
}
- (IBAction)convertToBinary:(id)sender {
int decNum = (int)textField.text;
if ([textField.text length] > 6) {
answer.text = #"Too many numbers entered.";
} else {
answer.text = [self binaryConvert:decNum];
}
[textField resignFirstResponder];
}
answer is a label.
int decNum = (int)textField.text; // WRONG
Should be:
int decNum = [textField.text intValue];
// my previous wrong answer:
// decNum -= value should be done everytime; not only on if ((decNum/value) >= 1)

Problem in String camparisson ...!

here is my code :
NSUInteger f;
for (f = 0; f < [appDelegate.books count]; f++) {
check = [appDelegate.books objectAtIndex:f];
checkthis = check.LotteryNumber;
mystring = check.LotteryNumber;
NSLog(#"Dynamic Value: %#",mystring);
NSLog(#"Static Value: %#",checkthis);
if (checkthis == mystring) {
found = YES;
break;
}
printf("In LOOP");
}
if ( found ) {
// do found
NSLog(#"Found");
} else {
// do not found
NSLog(#"not Found");
}
//if (checkthis == mystring) {
in above line if i place checkthis on both side , its working , but when i am taking a dynamic value its not working..
i also tried like this
if(checthis isEqualToString mystring)
same problem here ....
Thanks in advance
You're using pointer comparison and not string comparison.
You should be using:
if([checkThis isEqualToString:myString]) { ...
You are using comparision by pointer the comparison you made checks memory a ddresses and will only return true if its the same object being compared, you should use [string isEqualToString:otherString] method from NSString instead