getting NSArray length/count - Objective C - objective-c

Well I've looked at similar problems over the site but haven't reached a solution thus far so I must be doing something wrong.
Essentially, I am importing a text file, then splitting each line into an element of an array. Since the text file will be updated etc.. I won't every know the exact amount of lines in the file and therefore how many elements in the array. I know in Java you can do .length() etc.. and supposedly in Objective C you can use 'count' but i'm having no luck returning the length of my array... suggestions?
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"allshows"
ofType:#"txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [fileString componentsSeparatedByString:#"\n"];
NSUInteger *elements = [lines count];
NSLog(#"Number of Shows : ", elements);
and what is being output is NOTHING. as in "Number of Shows : " - blank, like it didn't even count at all.
Thank you for any help!

You're missing the format string placeholder. It should be:
NSLog(#"Number of shows: %lu", elements);

You need to use a format specifier to print an integer (%d):
NSLog(#"Number of Shows : %d", elements);

Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.

Related

Expression Result Unused (with array description)

Basically, I'm trying to print out an NSArray's description.
I'm getting 'Expression Result Unused', even though I've tried everything I could find online!
Does anyone know?
NSArray *WalletBalance= [responseDict objectForKey:#"balance"];
NSString *wBalance = (#"This wallet currently has %# dollars", [WalletBalance description]);
How could I append WalletBalance's description into a string?
You need to use:
NSString *someText = [NSString stringWithFormat:#"This wallet currently has %# dollars", [WalletBalance description]];
Courtesy of Append string with variable

How to pass the dictionary output value to the url in objective C?

I am working on a proof of concept app that scans a barcode and returns the barcode information as a UPC value(i.e. 9780596001612). I am confused on how to pass that return value to the url(I read some tutorials online but those doesn't seem to have what I am looking for). I have hardcoded the UPC value in the code and I am getting the right response but I want to be able to pass the return value from the barcode scan and pass that value to the url. I recently started working on Objective C and would greatly appreciate your help. Thank you.
- (void) onQRCodeScanned:(NSString*) result {
NSString *theJSONString = [result description];
NSError *theError = NULL;
NSDictionary *theDictionary = [NSDictionary dictionaryWithJSONString:theJSONString error:&theError];
NSArray *results = [theDictionary objectForKey:#"decodedString"];
//results returns 9780596001612
NSString *barcodeUrl = #"http://www.outpan.com/api/get_product.php?barcode=9780596001612";
NSString *resp = [self makeRestAPICall: barcodeUrl];
}
You need to format the string to include the barcode like this (I'm assuming the barcode is the first result of the results variable - it doesn't make sense in it's current format - and that first result does indeed exist):
NSArray *results = [theDictionary objectForKey:#"decodedString"];
NSString *barcode = [results firstObject];
NSString *barcodeUrl = [NSString stringWithFormat:#"http://www.outpan.com/api/get_product.php?barcode=%#",barcode];
NSString *resp = [self makeRestAPICall: barcodeUrl];
Notice how I using stringWithFormat: and then use %# to represent a 'variable' as such, followed by the actual variable I want to include?
You can include primitive types using placeholders like %d (int), %c (char). You use %# to represent an object, such as a NSString.
You can do this for any number of variables, but you'll get an error if the number of placeholders don't match the number of variables!
You may like reading this: String Format Specifiers

Having trouble taking an index of an array and making it an NSString

I get an array from a JSON and I parse it into an NSMutableArray (this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:#"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:#"label"];
NSLog(#"arrayLabel = %#", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(#"string = %#", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:#"%#", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:#"%#", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex: to an NSString. In your case, arrayLabel isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary. Likewise for arrays and NSArray and strings and NSString.
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.

Padding NSString not working

I have read that to left-pad an NSString all you need to do is this:
NSString *paddedStr = [NSString stringWithFormat:#"%-20.20# %-20.20#",
aString, anotherSting];
But, that does not work !! I donĀ“t know why. I have tried a lot of combinations without success. Examples:
NSString *paddedStr = [NSString stringWithFormat:#"%-20s#", " ", myString];
but that way is ugly and ... ugly. It just append 20 times the char (" ") before the string (myString) and that is not what we need right?
The goal is to have an NSString formatted to present two or more columns of 20 chars each one no matter the length of the string within a row.
Example Goal Output:
Day Hour Name Age
Does anybody know how to do this right?
I'm using ARC and iOS 5.
And actually, the formatted string is going to be written to file using NSFileHandle.
Thanks to all of you folks !!
Edit:
I have noticed that this works:
NSString *str = [NSString stringWithFormat:#"%-10.10s %-10.10s",
[strOne UTF8String], [strTwo UTF8String]];
But... We don't want C-style strings either.
Here is a way to do that :
NSString *paddedStr = [NSString stringWithFormat:#"%#%#",
[#"day" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0],
[#"Hour" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0]];

\n does not skip to next line in NSString

NSMutableString *a = #"Hi";
NSMutableString *b =[a stringByAppendingString:#"\n\n Hi Again"];
The above doesn't give an error but does not put "Hi Again" on the next line. Why?
EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.
I will leave this as it still maybe helpful.
Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.
You need to read the Documentation on NSMutableStrings.
to give you an idea
//non mutable strings
NSString *shortGreetingString = #"Hi";
NSString *longGreetingString = #"Hi Again";
/*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/
NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];
/*The mutableString, now uses an appendFormat to construct the string
each %# in the Parameters for the appendFormat is a place holder for values of NSStrings
listed in the order you want after the comma.
Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:#"%#\n\n%#",shortGreetingString,longGreetingString];
NSLog (#"mutableString = %#" ,mutableString);
[pool drain];
I think this might help you. You'd rather to use '\r' instead of '\n'
I also had a similar problem and found \n works in LLDB but not in GDB
Try using NSString. You could use:
NSString *a = [NSString stringWithFormat:#"%#\n\n%#", #"Hi", #"Hello again"]
If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0
myView.numberOfLines=0;