objective-c: variable is not a CFString, how to correctly assign string value to an object's property? - objective-c

- (IBAction)start:(UIButton *)sender {
NSString *t=#"123"; // line 1
self.detailDescriptionLabel.text=t;
}
The syntax check is correct, but when I build and run it on iphone, it breaks out at line 1 and sending the error message like "variable is not a CFString"
How to solve this problem?
And it is working when I wrote this:
- (IBAction)start:(UIButton *)sender {
self.detailDescriptionLabel.text=nil;
}
I really think the problem lies in how to correctly assign a string constant to a UILabel's text property.

Ok, it is stupid... I wrongly pressed the shotcut key and a breakpoint to it.....So actually there is no mistakes in the code, just my mistake in the XCode IDE.

Related

id object in theos mobilesubstrate tweak , i can't disable this -(id)

-(id)initWithUserSettings:(id)userSettings mqttSender:(id)sender;
NSDictionary *settings3=[[NSDictionary alloc] initWithContentsOfFile:#"/var/mobile/Library/Preferences/myfilehere.plist"];
if ([[settings3 objectForKey:#"invisiblechat"] boolValue]){
return NULL;
}
else{%orig;}
[settings3 release];
}
I don't know if this code is the right one, I am searching 2 days without an answer. On -(void) this code works really good and it keeps the -(void) disabled until the objecForKey is disabled. What I want is to keep the (id)mqttSender disabled/enabled with the toggle, I can't do it though. Any help please?
Im not quite sure what you are trying to achieve from your explanation but if your going to init a class that is a subclass of NSObject you need to be at least call [super init] and return self, if you dont whatever is calling the init method will fail to get a reference to that object.
Setting a return value of 'void' means that the method shouldn't return anything, if you set 'id', then 'id' is a representative of a value that you haven't defined so it can be nearly anything.
With your method you are setting a return value of 'id' which means you should be returning something from that method. At the moment you have a single return inside of your 'if' statement, but after that your method is open ended and doesn't return anything. Xcode will throw up a compile error in this case because you are failing to return anything and it's expecting at least something to come out of the method, even if it's nil.
One other thing: you only end a method deceleration with a semi-colon when defining a method/function, when you implement the method/function you don't need a semi-colon as you are following it with curly braces which contain your code for that method/function.

Crash from attempting to clear a calcuator

Hey I'm trying to make a clear button for my polish calculator.. code give me this error when executing the clear button
unrecognized selector sent to instance 0x6a6e1e0'
there is a button in the interface builder linked to clearBtn
this code is in the CalculatorViewController.m
display is linked to the UILabel
and..
heres the code
- (IBAction)clearBtn
{
if (self.userIsInTheMiddleOfEnteringANumber) {
[[self display] setText:#"0"];
[self setUserIsInTheMiddleOfEnteringANumber:NO];
}
}
- (IBAction)clearAllBtn //this button works fine..
{
[[self brain] clearAll]; // the brain class has a method to set the array to nill
[[self display] setText:#"0"];
[self setUserIsInTheMiddleOfEnteringANumber:NO];
}
kind of hard to know without seeing the rest of your code, but I'd assuming it's because you don't have a method named setUserIsInTheMiddleOfEnteringANumber. Maybe double check the spelling and parameter list?
Thanks for posting the code. Generally, unrecognized selector errors are not tough, but we need to know which line of code is triggering it, which will help us understand which object is receiving the message, and what the selector is. Can you post a dump of the error messages?
Read about Objective-C selectors here.
Generally speaking, Interface Builder is a great tool for... umm, building interfaces. But you do have to be careful about connections and making sure everything is still linked up after making changes to your code or layout.

Change label text based on textfield

I'm making a little demo app and I'm having trouble changing.
Heres the thing:
I have a UIButton that every click will add a character in a NSString in a UITextField.
And I put an IBAction(Mudar_Resposta) on that UITextField(campo) in the part 'Value Changed'.
In that IBAction, I put that:
- (IBAction)MudarResposta:(id)sender {
campo.text=#"lol";
}
But I can't get it to work. Any ideas?
Thanks.
The proper way to get it to work is to connect an action method to the Touch Up Inside event for your button. Within that method, edit they text for your text field.
- (IBAction)mudarResponsta:(id)sender { // Connect to Touch Up Inside of your button
campo.text = [NSString stringWithFormat:%# %#", campo.text, #"lol"]; // Add string ' lol' every time it's called
}
Connecting to Value Changed will never invoke the method because the value is never being changed—you're connecting the changing method to the change…if that makes any sense.
Most likely you didn't connect that action to anything in the XIB file.
- (IBAction)Button:(id)sender {
[self.contacts addObject:_txtField.text];
_txtField.text=#"";
}
Here contacts is an Array name which is a Mutable Array. txtField is UITextField.

Error Objective C forControlEvents

I am new to objective C and am having a problem with the following line. The error is: "Passing argument 3 of 'addTarget;action:forControlEvents:' makes integer from pointer without a cast." The wierd thing is that I get the warnings and everything works fine on the IPAD but on the simulator it works sometimes and other times it does not work. I have a textfield in a tableviewcell, when the user types it sends what they typed to a variable.
[textField addTarget:self action:#selector(setFilterCriteria:) forControlEvents:UITextFieldTextDidChangeNotification];
Here is the IBAction:
- (IBAction)setFilterCriteria:(id)sender {
UITextField *senderTextField = (UITextField *)sender;
appDelegate.setFilterCriteria = senderTextField.text;
}
You're specifying the wrong kind of thing for the control event.
UITextFieldTextDidChangeNotification is a notification name, not a control event type. You want one of the UIControlEvent... constants, like UIControlEventValueChanged.
(You're seeing the warning because the notification names are NSString*s, and the expected type of that argument is a defined numeric constant.)

String Help - Objective C

I'm trying to make an app that will respond to your command when inserted. So you type in any text in the first box and press enter. It will respond with a response in the 2nd field. I'm not sure how the coding is done here. I'm having trouble with the "if inputbox text = #"whatever", I'm pretty sure that is completely off. Here is the code I have so far (not iphone sdk):
#import "HeliosControl.h"
#implementation HeliosControl
- (IBAction)quitButton:(NSButton *)sender {
}
- (IBAction)sendButton:(NSButton *)sender {
if (inputBox *********) // <------ What do I put in for the asterisks?
{
[outputBox setStringValue:#"Welcome to the SYSTEM"];
}
else
{
[outputBox setStringValue:#"I do not understand your command."];
}
}
#end
BTW I'm a complete noob since I started Objective-C like a week ago.
Second Question:
This is a very simple one, but what would be the coding for closing an application? This is for my quit button.
You want if ([[inputBox stringValue] isEqualToString:#"whatever"]) (assuming inputBox is an NSTextField — otherwise, use the appropriate method for that class to get a string out of it).
Oh, and you can quit the application with [NSApp terminate:self].
Chuck's answer is spot on, but I thought it worth expanding on why you've had problems. There are a number of mistakes in your line:
"if inputbox text = #"whatever"
a) In Objective C you have to use == to check if x is equal to y. So the if statement would be:
if (myFirstVariable == mySecondVariable) { // Do something }
b) A string variable is actually a more complicated thing than a variable just holding a number. That variable's value will actually be the memory address where it is stored. Also, you will usually actually only be using a pointer (denoted by the * when you declare a variable) to the variable.
This means that if you type the following:
if (myFirstVariable == #"Some text")
or
if (myFirstStringVariable == mySecondStringVariable)
Then you're actually only checking for whether they both point to the same bit of memory! Not whether the text is the same. This is why as Chuck explained you need to use the [isEqualToString] method.
Hope that helps!