Objective-C compiler error - objective-c

Need help with the [currentValue, targetValue, difference]; line. It says that initializer element is not a compile-time element. Please post solution.
NSString *message = [NSString stringWithFormat:
#"The value of the slider is: %d\nThe target value is: %d\nThe difference is: %d",
[currentValue, targetValue, difference];
UIAlertView *alertView = [[UIAlertView alloc]]
initWithTitle:#"Hello, World!"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];

Should be:
NSString *message = [NSString stringWithFormat: #"The value of the slider is: %d\nThe target value is: %d\nThedifference is: %d", currentValue, targetValue, difference];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hello, World!"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
You have an extra '[' before current and and extra ']' after the UIAlertView alloc.

Related

Terminating app due to uncaught exception 'NSInvalidArgumentException' 5

i am getting this error while running my app:
my code is:
- (IBAction)yes:(id)sender {
UIAlertView *msgbox=[[UIAlertView alloc]initWithTitle:#"Warrning" message:nil delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
if ([_usernametextbox.text length]>0)
{
if ([_passwordtextfield.text length]>0)
{
NSMutableString *ms=[[NSMutableString alloc]initWithFormat:url,_usernametextbox.text,_passwordtextfield];
NSURL *serviceurl=[[NSURL alloc] initWithString:ms];
NSError *error=nil;
NSData *data=[NSData dataWithContentsOfURL:serviceurl options:NSDataReadingUncached error:&error];
NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
NSString *jsondata=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJSON *parsedata=[[SBJSON alloc] init];
dic=[parsedata objectWithString:jsondata error:nil];
if([dic count]>0)
{
NSMutableString *st=[[NSMutableString alloc]initWithString:[dic objectForKey:#"loginResult:#""+"]]; //#" " withString:#"+"
if ([st isEqualToString:#"YES"])
{
UIAlertView * alert=[[UIAlertView alloc] initWithTitle:#"accept" message:#"correct username" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView * alert=[[UIAlertView alloc] initWithTitle:#"Error" message:#"Invalid UserName and Password" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
else
{
msgbox.message=#"Please Enter Password...";
[msgbox show];
}
}
else
{
msgbox.message=#"Please Enter Username...";
[msgbox show];
}
I get the error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[NSConcreteData
initWithContentsOfURL:options:error:]: nil URL argument'
on the line:
NSMutableString *st=[[NSMutableString alloc]initWithString:[dic objectForKey:#"loginResult:#""+"]]; //#" " withString:#"+" if ([st isEqualToString:#"YES"])

Accessing a dictionary entry in an array

Basically I have an array with 3 rows, each row is a NSDictionary with 6 elements (nameLabel, name, colorLabel, color, priceLabel, price).
Example:
NSDictionary* row1 = [[NSDictionary alloc]
initWithObjectsAndKeys:#"MacBook",#"Name",#"White",#"Color",#"$1200",#"Price", nil];
Then I add those elements into an array:
NSArray* array = [[NSArray alloc] initWithObjects:row1,row2,row3, nil];
//row2 and row3 being the same as row1 but with different strings.
And then I add them to a table with subviews.
I'm trying to write an alert, which is working, but written like this, it shows a whole row in between {}. (Ex: You selected {Color = Black; Name = iPhone; Price = $600; }.
NSUInteger row = [indexPath row];
NSString *rowValue = [computers objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
#"You selected %#", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:nil];
[alert show];
Instead of that, I would like to only display the name of the selected element.
I realized I need to change this code segment, but don't know how to access the name element in the dictionary.
NSString *message = [[NSString alloc] initWithFormat:
#"You selected %#", rowValue];
NSUInteger row = [indexPath row];
NSDictionary *dictionary = [array objectAtIndex:row];
NSString *name = [dictionary objectForKey:#"Name"];
NSString *message = [[NSString alloc] initWithFormat:#"You selected %#",name];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:nil];
[alert show];

UIAlertView with timestamp

I am a newbie to Objective-C. I would like to display the current date and time in an alert view.
- (IBAction)postButtonPressed:(UIButton *)sender {
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"MMM d, yyyy h:mm" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message Posted" message:#"Your Message is posted on: #todayString" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
}
Your alert message string needs to be redone. Try this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message Posted"
message:[NSString stringWithFormat:#"Your Message is posted on: %#", todayString]
delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
You can concate NSString using stringWithFormat: or stringWithString: there's also other appeding functions available, see doc
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message Posted" message:[NSString stringWithFormat:#"Your Message is posted on: %#",todayString] delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release]; //don't forget to release UIAlertView object if you don't using ARC
Try
NSString *str = [NSString stringWithFormat:#"Your Message is posted on: %#", todayString];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message Posted" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];

encoding U00d8\U00aa\ NSString

I'm receiving remote notification with string
\U00d8\U00aa\U00d8\U00aa\U00d8\U00a7\U00d8\U00a7"
I would like to display this string in alert dialog.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Alert"
message:/*alertValue*/decodedString
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
but it oesn't shown correctly, what i need to do to encode it correctly?
I figure it out:
NSString *decodedString = [NSString stringWithUTF8String:[alertValue cStringUsingEncoding:NSUnicodeStringEncoding]];

Pull more than 1 echo from PHP

OK, I have a login screen that sends username and password to a PHP page. The PHP page echo's Yes or No based on the login status, but I would like it to echo the user id from the database for further use. Do I have to use JSON for this? I dont know how to retrieve more than one echo's. Currently the code is:
- (IBAction) login: (id) sender
{
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",usernameField.text, passwordField.text];
NSString *hostStr = #"https://www.mysite.com/login.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:#"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Business Manager" message:#"Login Successful"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
RootViewController *viewMenu = [[RootViewController alloc]
initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
self.rootViewController = viewMenu;
[viewMenu release];
[self.navigationController pushViewController:self.rootViewController animated:YES];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Username or Password Incorrect"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
}
Now I just want to pull one more echo called "ID". Forming JSON for this seems arbitrary.
Technically you can use whatever response format you want -- though it's best to stick with something standard as you'll be able to find libraries made for you already. A REST service with a JSON response is a pretty easy way to facilitate communication between a server and client, so it would probably be a good idea to start there.