Objective-C Noob: How to define + call a method in main? - objective-c

I wrote a method that works inside of an object, but now I want to extract it so that it's just a function. This is my broken command line tool program:
#import <Foundation/Foundation.h>
+ (NSMutableString *)reverseString:(NSString *)originalString {
NSMutableString *reversedString = [[NSMutableString alloc] init];
for (NSInteger i = originalString.length; i > 0; i--) {
[reversedString appendFormat:#"%c", [originalString characterAtIndex:i-1]];
}
return reversedString;
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSString *originalString = #"original string";
NSMutableString *newString = [reverseString:originalString];
NSLog(#"Reversed string: %#", newString);
}
return 0;
}
My question is, how would I call the reverseString method from main()? I don't think I'm defining it properly. Do I have to declare it too? I know that the contents of my method work fine, but I don't know how to define it in a way that allows main to see it.

A "method" is, by definition, part of a class. There are two types, instance methods and class methods. To invoke an instance method, you need, well, an instance of the class. To invoke a class method, you don't need an instance. You can just invoke it directly on a class.
By contrast, there are also "functions". You don't need an instance or a class to invoke a function.
So, it sounds like you want a function. Functions are something that Objective-C inherits from C. The syntax for functions is different from the syntax for methods. Here's how your code might look using a function:
NSMutableString* reverseString(NSString *originalString) {
NSMutableString *reversedString = [[NSMutableString alloc] init];
for (NSInteger i = originalString.length; i > 0; i--) {
[reversedString appendFormat:#"%c", [originalString characterAtIndex:i-1]];
}
return reversedString;
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSString *originalString = #"original string";
NSMutableString *newString = reverseString(originalString);
NSLog(#"Reversed string: %#", newString);
}
return 0;
}
By the way, your code does not "work fine". You can't iterate through a string by what it calls "characters" and treat all of them as independent. What NSString calls "characters" are actually UTF-16 code units. Not all Unicode characters can be expressed as single UTF-16 code units. Some need to use two code units in what's called a surrogate pair. If you split up and reverse a surrogate pair, you'll get an invalid string.
As a separate issue, Unicode has combining marks. For example, "é" can be expressed as U+0065 LATIN SMALL LETTER E followed by U+0301 COMBINING ACUTE ACCENT. Again, if you reorder those "characters", the accent will combine with a different character (or fail to combine at all).
The correct way to iterate through the composed character sequences of a string is to use the -[NSString enumerateSubstringsInRange:options:usingBlock:] method with the NSStringEnumerationByComposedCharacterSequences option.

By "I want to extract it so that it's just a function" you're implicitly saying "I want a C-style function, not an Objective-C class method". C-style functions are declared and called differently (blame history).
static NSMutableString * reverseString(NSString * originalString) {
...
}
...
NSMutableString *newString = reverseString(originalString);

Related

Is it a variable or an object? Im very confused

#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
#autoreleasepool {
NSString *str = #"Programming is fun";
NSLog (#"%#", str);
}
return 0;
}
In the line
NSString *str = #"Programming is fun";
the constant string object Programming is fun is assigned to the NSString variable str. Its value is then displayed using NSLog .
The NSLog format characters %# can be used to display not just NSString objects, but other objects as well.
/*****/
The previous paragraph was from a book I read, what is really confusing to me is why is he keep using the words variable and objects interchangeably? are objects and varaibles the same thing? so far this is the only confusing part about obj-c to me.
please explain, thank you
An object is an instance of a class. Something that is allocated in memory.
A variable is a name which you use to access something like an object (NSString for example) or a primitive (int for example).
In your case so your object is an instance of NSString, that contains #"Programming is fun":
NSString *str = #"Programming is fun";
The variable to access that object is str.

Nested NSArray for loop won't loop in nested fashion

I've been learning Objective-C for five days and I have only 2 weeks of prior programming experience so please make answers as simple as possible.
I'm doing an exercise in a book that asks me to generate a list of proper names that are also regular words. To do this I have am running a for loop for each proper name from a NSArray proper name object. Within that for loop I have a nested for loop testing each name against each word in an NSArray regular word object using the caseInsensitiveCompare method.
Here is my code:
import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
//Gets the sting with proper names
NSString *propername = [NSString stringWithContentsOfFile:#"/usr/share/dict/propernames" encoding:
NSUTF8StringEncoding error:NULL];
//Gets the string with regularwords
NSString *inpropername = [NSString stringWithContentsOfFile:#"/usr/share/dict/words" encoding:
NSUTF8StringEncoding error:NULL];
NSArray *proper = [propername componentsSeparatedByString:#"/n"];
NSArray *inproper = [inpropername componentsSeparatedByString:#"/n"];
for (NSString *n in proper){
NSLog(#"%#", n);
for(NSString *i in inproper){
NSLog(#"%#", i);
if ([n caseInsensitiveCompare:i] == NSOrderedSame)
{
NSLog(#"Yahooo! Got One! %#", n);
}
}
}
}
return 0;
}
Instead of the for loops running in a nested fashion they are running in a sequential fashion. The output is like this:
Aaron
all the names...
Yvonne
a
all the regular words....
Zyzzogeton
Any ideas for why the nested for loop is not running in a nested fashion?
The code is correct except you are not breaking the files into words since you are using "/n" instead of "\n".
This means that each array contains exactly one element which is a string with all the words in it.

Objective-C--Parse NSString to a callable method

Could I parse an NSString to have it call a method that matches its name in Objective-C? Here is an example:
If I had a string called doSomething, and I had a method called -(void) doSomething, could I do something like scanf to parse whatever text I typed in to check if there were any method matching it, and if yes then call that method?
Try NSSelectorFromString(#"methodName"); and the associated NSObject methods like respondsToSelector:
jxpx777's answer will give you the information you were looking for, but in case you want more, the runtime has a long list of C functions that provide a fairly complete introspection of objects and classes.
For instance, if you want an NSArray of method names implemented by a class, you can do something like this:
Class myClass = [self class];
unsigned int methodCount;
Method *methods = class_copyMethodList(myClass, &methodCount);
NSMutableArray *methodNames = [NSMutableArray arrayWithCapacity:10];
for (int i = 0; i < methodCount; i++) {
const char *methodNameCStr = sel_getName(method_getName(methods[i]));
NSString *methName = [NSString stringWithCString:methodNameCStr
encoding:NSASCIIStringEncoding];
[methodNames addObject:methName];
}
free(methods);
NSLog(#"Methods: %#", methodNames);
You will notice that plain C calls and Objective-C/Cocoa are mixed freely.

obj-c addiction function to NSString

I'm using a declaration of function that converts NSData into hexadecimal NSString
#implementation NSString (Hex)
+ (NSString*) hexStringWithData: (unsigned char*) data ofLength: (NSUInteger) len
{
NSMutableString *tmp = [NSMutableString string];
for (NSUInteger i=0; i<len; i++)
[tmp appendFormat:#"%02x", data[i]];
return [NSString stringWithString:tmp];
}
#end
What is name of this procedure?
(I mean how is called this #implementation into/of class which I haven't defined and where is documentation ?)
What purpose has (Hex) part of implementation ?
Thanks
That is a class cateogry that provides a way to add extra methods to existing classes without the need of subclassing. Hex is the name of this particular category because a single class can have multiple categories. Special consideration should be taken when creating categories because it is possible to override existing or future methods.

Objective c implement method which takes array of arguments

Hee
Does anybody know how to implement an method in objective c that will take an array of arguments as parameter such as:
[NSArray arrayWithObjects:#"A",#"B",nil];
The method declaration for this method is:
+ (id)arrayWithObjects:(id)firstObj...
I can't seem to make such method on my own. I did the following:
+ (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes;
[SomeClass doSometing:#"A",#"B",nil manyTimes:2];
It will give the warningtoo many arguments to function 'doSometing:manyTimes:'
Thanks already.
The ellipsis (...) is inherited from C; you can use it only as the final argument in a call (and you've missed out the relevant comma in your example). So in your case you'd probably want:
+ (void)doSomethingToObjects:(id)firstObject, ...;
or, if you want the count to be explicit and can think of a way of phrasing it well:
+ (void)doManyTimes:(NSInteger)numberOfTimes somethingToObjects:(id)firstObject, ...;
You can then use the normal C methods for dealing with ellipses, which reside in stdarg.h. There's a quick documentation of those here, example usage would be:
+ (void)doSomethingToObjects:(id)firstObject, ...
{
id object;
va_list argumentList;
va_start(argumentList, firstObject);
object = firstObject;
while(1)
{
if(!object) break; // we're using 'nil' as a list terminator
[self doSomethingToObject:object];
object = va_arg(argumentList, id);
}
va_end(argumentList);
}
EDIT: additions, in response to comments. You can't pass the various things handed to you in an ellipsis to another function that takes an ellipsis due to the way that C handles function calling (which is inherited by Objective-C, albeit not obviously so). Instead you tend to pass the va_list. E.g.
+ (NSString *)doThis:(SEL)selector makeStringOfThat:(NSString *)format, ...
{
// do this
[self performSelector:selector];
// make string of that...
// get the argument list
va_list argumentList;
va_start(argumentList, format);
// pass it verbatim to a suitable method provided by NSString
NSString *string = [[NSString alloc] initWithFormat:format arguments:argumentList];
// clean up
va_end(argumentList);
// and return, as per the synthetic example
return [string autorelease];
}
Multiple arguments (also known as an arglist) can only come at the end of a method declaration. Your doSomething method would look something like this:
+ (void)doNumberOfTimes:(NSInteger)numberOfTimes withStrings:(id)firstArg, ...
{
va_list args;
va_start(args, firstArg);
NSString * argString = firstArg;
while (argString != nil)
{
// do something with argString here
argString = va_arg(args, NSString *);
}
va_end(args);
}
To be called as follows:
[SomeClass doNumberOfTimes:2 withStrings:#"A", #"B", nil];
See also: How to create variable argument methods in Objective-C
I think you're after a variadic function. Here's Apple's documentation: http://developer.apple.com/library/mac/qa/qa2005/qa1405.html