Converting an object of an array - nsmutablearray

I have the following block of codes in my program.
for(int k=0;k<reqroom.count;k++)
{
NSString *rent=[roomRent objectAtIndex:k];
NSString *tax=[roomTax objectAtIndex:k];
NSString *no=[textvaluearray objectAtIndex:k];
NSDecimalNumber *rentd=[NSDecimalNumber decimalNumberWithString:rent];
}
//here roomRent,roomTax and textvaluearray are NSMutableArrays
This is the error that I am receiving:
'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x81b2ce0'
Thanks in advance.

From the exception "-[__NSArrayI length]:" it looks like "rent" object is not a string type and its array. Can you check the type of "rent" before converting to decimal.

What is reqroom? Is it an NSString or NSArray (or NSMutableArray)? If it's some sort of array and you want to iterate as many times as there are objects in the array, you should probably do something like
for(int k=0;k<[reqroom count];k++)

Related

Swift: Converting NSNumber into NSString (NSNumber is not a subtype of NSString)

In my app, I'm going into CoreData and grabbing an entry whose type is a Double, then I'm trying to put that value into a text field in my app.
This looks like
lengthTextField.text = lastSession.length but I'm getting the error NSNumber is not a subtype of NSString
The value of lastSession.length is 6.0 for reference. Any suggestions on how to properly put that data in my text field?
Thanks!
In core data numbers are backed by NSNumber. You can view the documentation here
In swift you can access the string representation of the number through the instance property stringValue
lengthTextField.text = lastSession.length.stringValue
You'll have to format the NSNumber's doubleValue member to a string before outputting.
let x:NSNumber = 6.0
let s:String = String(format:"%f", x.doubleValue) //formats the string to accept double/float
println(s)

objective c appending string cocos2d

Hello I am writing an application and I want to create a string.
so I use that
NSString *scoreString;
NSString *cupCakesPassedString;
NSString *cupCakes;
int cupCakesPassed;
int totalCupCakesPerLevel;
-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
if (sprite.position.y <= 0) {
sprite.position = ccp( sprite.position.x,768 );
score+=5;
scoreString=[NSString stringWithFormat:#"%i",score];
[label setString:scoreString];
cupCakesPassed++;
cupCakesPassedString=[NSString stringWithFormat:#"%i",cupCakesPassed];
cupCakes=[[cupCakesPassedString stringByAppendingString:#"/"]stringByAppendingString:totalCupCakes];
[passingCupCakes setString:cupCakes];
}
}
it crashes!! but if use another string like scoreString it works...
in init method I have
totalCupCakesPerLevel=30;
scoreString=[NSString stringWithFormat:#"%i",score];
cupCakesPassedString=[NSString stringWithFormat:#"%i",cupCakesPassed];
totalCupCakes = [NSString stringWithFormat:#"%i",7];
cupCakes=[[cupCakesPassedString stringByAppendingString:#"/"]stringByAppendingString:totalCupCakes];
if I do this
cupCakes=[[cupCakesPassedString stringByAppendingString:#"/"]stringByAppendingString:scoreString];
I also has in init method that
cupCakes=[[cupCakesPassedString stringByAppendingString:#"/"]stringByAppendingString:totalCupCakes];
and actually works...until the method is called.
the numbers might be wrong but are for testing purposes
it seems that the problem is with string totalCupCakes,since even if I use #"test" works but what is wrong with that string?
Using a string method like this:
cupcakes = [NSString stringWithFormat:#"%d/%d", cupCakesPassed, totalCupCakes];
Would be simpler to use instead of the appending strings.
If I understand right, all your problems are because of non-retained strings. All stringWith... constructors of NSString class returns autoreleased objects. Retain them after creation and release in your dealloc method.
In your case to the moment of calling method, strings are deallocated and are not valid objects

How to store last part of string?

I have a string as http://www.google.co.uk/ig/images/weather/partly_cloudy.gif.
I need to save only the partly_cloudy.gif .How do i select obly that part of string?
If you are sure that it represents a path then you can call the lastPathComponent method on it.
Usage
NSString * link = #"http://www.google.co.uk/ig/images/weather/partly_cloudy.gif";
NSLog(#"%#", [link lastPathComponent]);
Use the NSString function componentsSeparatedByString. For example, NSString *url = #"http://www.google.com/1/2/3/test.gif"; NSArray *components = [url componentsSeparatedByString:#"/"]; would give you an NSArray of all strings in between a '/' character.
NSString reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

NsMutable Array problem

when i remove object from nsmutable array it shows exception but some time it works
[Array removeObjectAtIndex:row];
i have use this code
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
Array is type of NSarray ... it is object of NSArray it should be object of NSMutableArray and not NSArray

NSString within a link

I have a UIWebView that loads a link, http://www.google.com/a/datacommsales.net. But I want to have the datacommsales.net part interchangable. What I would like it to be is http://www.google.com/a/stringOne, stringOne being the NSString, so I can set the value of the string and change the link without editing the code. But the link is inside quotes, #"http://www.google.com/a/datacommsales.net", so it doesn't recognize the string. How could I include the string's value as part of the link? Any help is appreciated.
EDIT
To be a little more specific, here's my code:
- (IBAction)refreshNow:(id)sender
{
NSString *variablePart = #"secondpart.com";
NSString *page = [NSString stringWithFormat:#"google.com/a/variablePart/docs"];
[webView loadHTMLString:page baseURL:nil];
}
How would I put the string variablePart in the link like that?
do you mean just something like
NSString *variablePart = #"secondpart.com";
NSString *url = [#"http://www.google.com/" stringByAppendingString:variablePart];
EDIT:
NSString *variablePart = #"secondpart.com";
[NSString stringWithFormat:#"google.com/a/%#/docs", variablePart];
Checks the NSString stringWithFormat method... Or explain a little more how you do your stuff...

Resources