how to make one string from several? Xcode - objective-c

I make some method, that takes digits and operands from stack and display it in a more user friendly style. The problem is with descriptionString variable, it return null in part when topOfStack is "+". I show a log below.
+(NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSString *descriptionString;
id topOfStack = [stack lastObject];
NSString *secondInStack;
NSString *thirdInStack;
if (topOfStack)
[stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}
return descriptionString;
}
i made example with 2 + 6, this is log:
2012-02-21 22:09:39.983 Calculator[12536:f803] stack = (
2,
6,
"+"
)
2012-02-21 22:09:39.983 Calculator[12536:f803] description is (null)
Why descriptionString is null? Where i made a mistake? Thanks

In the line:
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
The variable descriptionString is nil. Replace that line with the following.
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];

I suspect you are not setting a value to the descriptionString variable in the other branch of the if, so you are appending string to null. You could set the variable, or you could use [NSString stringWithFormat:format]:
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}

Related

Issue AVSpeechUtterance

I´m trying to make the AVSpeech speak up two NSString in the string *combine and pas it to AVSpeech. But it´s only *speekone that get passed to the *combine string. So I only ge the first string spoken. Am I declaring the string wrong somehow or do I need to change my method?
- (IBAction)UIButtonPlayPressed:(UIButton *)sender{
NSString *speekone = _activity.activityName;
NSString *speektwo = _activity.activityDescription;
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
if (speechPaused == NO) {
//Title for button [self.UIButtonPlay setTitle:#"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
NSLog(#"playing");
} else {
//Titleforbutton [self.UIButtonPlay setTitle:#"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSLog(#"paused");
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:combined];
[self.synthesizer speakUtterance:utterance];
The first argument to your stringWithFormat is not a format specifier.
This line:
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
should become:
NSString *combined = [NSString stringWithFormat:#"%# %#", speekone, speektwo];

How to check the NSString contains URL or string data?

I am fresher to iOS, i am getting problem at checking string object contains URL or string?
NSMutableArray *Arr=[NSMutableArray alloc]initWithObject:#"Welcome", #"http://abcd.com/Images/bus.png", nil];
int i;
i++;
NSString *str=[Arr objectAtIndex:i];
Now, i want to check condition, if string contains "Welcome", have to display on label or if it is URL , i need to display that URL image in ImageView. So how can i check it? Please help me in this problem.
Instead of initiating both as NSStrings, try differentiating between them by making urls a NSURL (special container specifically for urls):
NSMutableArray* Arr = [NSMutableArray alloc]initWithObject:#"Welcome", [NSURL URLWithString:#"http://abcd.com/Images/bus.png"], nil];
for(id object in Arr)
{
if([object isKindOfClass:[NSString class]])
{
NSString* string = object;
NSLog(#"String: %#", string);
}
else if([object isKindOfClass:[NSURL class]])
{
NSURL* url = object;
NSLog(#"URL: %#", url);
}
}
Try like this
NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:#"Welcome", #"http://abcd.com/Images/bus.png",nil];
NSString *st=nil;
for(NSString *string in Arr)
{
NSArray *matches = [detector
matchesInString:string
options:0
range:NSMakeRange(0,
[string length])];
for (NSTextCheckingResult *match in
matches) {
if ([match resultType] ==
NSTextCheckingTypeLink) {
NSURL *url = [match URL];
} else
{
NSlog(#"it is a string");
}
}
}
Try this, it will help you:
NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:#"Welcome", #"http://abcd.com/Images/bus.png", nil];
if([Arr count])
{
for (NSString *str in Arr)
{
if([str isEqualToString:#"Welcome"])
{
NSLog(#"str is %#",str);
//do whatever you want
}
if([str isEqualToString:#"http://abcd.com/Images/bus.png"])
{
NSLog(#"str is %#",str);
//do whatever you want
}
}
}
To check NSString is containing a URL You can Try This code
if ([stringName hasPrefix:#"http://"] || [stringName hasPrefix:#"https://"]) {
//show imageVivew
}

CS193P - Assignment 2, descriptionOfTopOfStack:, recursive class method

below is a class method that always returns null -when, for example, stack = 2,2,"+" I want it to return "2+2"
In the initial iteration the method correctly determines the topOfStack is a NSString rather than a NSNumber but is doesn't create NSString 'description' to equal "2+2" by recursive calling
I feel I'm missing something obvious here, am I dealing with the strings correctly....
+ (NSString *) descriptionOfTopOfStack: (NSMutableArray * ) stack
{
NSString *description;
id topOfStack = [stack lastObject]; // get last object
if (topOfStack) [stack removeLastObject]; // then remove it from stack
if ([topOfStack isKindOfClass:[NSNumber class]]) { // is last object a number?
return [topOfStack stringValue]; // if so then return it, **done***
}
else if ([topOfStack isKindOfClass:[NSString class]])
{
if ([topOfStack isEqualToString:#"+"])
{
[description stringByAppendingString: [self descriptionOfTopOfStack:stack]];
[description stringByAppendingString:#"+"];
[description stringByAppendingString: [self descriptionOfTopOfStack:stack]];
}
}
NSLog(#"Description is %#", description);
return description;
}
Method stringByAppendingString: returns an autoreleased string, does not modify the original one. If you want to modify description you must do something like
description = [description stringByAppendingString: [self descriptionOfTopOfStack:stack]];
Besides, writing
NSString *description;
you are just creating a pointer to a NSString, not a NSString. Use instead
NSString* description = #"";

my xmlDictionary its allway (null)

im trying to get the same value on xmlDictionary and dicionarioXML but, my dicionarioXML its allway (null), any help?
#synthesize xmlDictionary;
-(NSString*)buscaDados:(NSData*) dados
{
NSString * responseContent = [[NSString alloc] initWithBytes:[dados bytes] length:[dados length] encoding:NSUTF8StringEncoding];
NSError *parseError = nil;
xmlDictionary = [XMLReader dictionaryForXMLString:responseContent error:&parseError];
[responseContent release];
NSString* sucesso=[xmlDictionary valueForKeyPath:#"receitas.total.text"];
NSLog(#"xmlDictionary: %#",xmlDictionary);
return sucesso;
}
-(NSDictionary*)trataDados
{
NSDictionary* dicionarioXML = [self xmlDictionary];
NSLog(#"dicionarioXML: %#",dicionarioXML);
return dicionarioXML;
}
Null means something doesn't exist. What error are you getting back?
NSLog(#"Error: %#", parseError);

touchXML: stringValue not saved as string?

NSLog(#"Status: %#", [[[xmlElement elementsForName:#"status"] objectAtIndex:0] stringValue]);
NSString *val = [[[xmlElement elementsForName:#"status"] objectAtIndex:0] stringValue];
if (val == #"error")
{
This is my log 2011-07-02 16:09:40.565 Revistero[994:207] Status: error
However it doesn't enter the if
Any suggestion?
You should use isEqualToString:
if ([val isEqualToString:#"error"])
{