I want to do the following:
["Hello <firstname> <middlename> <lastname>". replaceTokensWithStrings:
#"firstname", someFirstName,
#"middlename", middleNameMightBeNilObject,
#"lastname", lastNameObject];
It looks like this is not possible because of the "nil as a terminator problem".
However NSLog also takes multiple arguments and can handle nil as parameters too:
NSString *nilValue = nil;
NSLog(#"Value of nilValue=%#", nilValue);
output
Value of nilValue=(null)
So how can I achieve this in my replaceTokensWithStrings:... method?
Update: The signature of my method:
-(NSString *)replaceTokensWithStrings:(NSString *)input, ... NS_SOMETHING_SPECIAL_HERE{
NSLog counts the number of format specifiers (%#, %i, etc.) in its format string (the first argument, the literal string) in order to know how many arguments it should pull off the call stack. This is why it can handle nil arguments -- it is using the count to terminate its processing (and, incidentally, why it will often crash if you give it too few arguments).
Since you have a format-type string, you can do the same thing -- just whip up a helper method that counts the number of <something> elements in the string on which replaceTokensWithStrings: has been called, and use the results to limit your processing of the va_list.
The NS_SOMETHING_SPECIAL in your method declaration would be NS_REQUIRES_NIL_TERMINATION for the case where you're using nil as a sentinel. You don't need anything special if you're getting the count of arguments from somewhere.
Can't you change nil to be #"" so it will replace with a blank string?
Don't work?
["Hello <firstname> <middlename> <lastname>". replaceTokensWithStrings:
#"firstname", #"Peter",
#"middlename", #"", // or myStr
#"lastname", #"Smith"];
or check value:
#"middlename", myStr ? myStr : #"",
EDIT:
If you want variable parameters, you should read http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
Related
We can extract words from a Swift string like this:
s.enumerateSubstringsInRange(s.characters.indices, options: .ByWords) {
(w,_,_,_) in print(w!)
}
but the forced unwrapping is generally a code smell. It is there because the first parameter of the callback is a String? (an optional). I've tried several possible strings in order to force the function to pass nil to the callback (e.g. the empty string, and strings with no word characters) but with no luck!
So I was wondering why the callback takes an optional string. Is there something I overlooked? Is it because a mutable string can be passed in and modified concurrently? If so, then would it be acceptable practice, if I know my original string is a constant (defined with let), to do the forced unwrap?
(The following information is from the response to my question
https://forums.developer.apple.com/thread/28272 in the
Apple Developer Forum.)
You can pass the .SubstringNotRequired option to enumerateSubstringsInRange(), and then the closure will be called with
substring == nil. This option is documented as
NSStringEnumerationSubstringNotRequired
A way to indicate that the block does not need substring, in which
case nil will be passed. This is simply a performance shortcut.
Example:
let str = "Hello, playground"
str.enumerateSubstringsInRange(str.characters.indices,
options: [.ByWords, .SubstringNotRequired]) {
substring, substringRange, _, _ in
print(substring, substringRange)
}
Output:
nil 0..<5
nil 7..<17
I think it is safe to assume that substring != nil if the
.SubstringNotRequired option is not given.
I'm a little bit confused about the syntax of NSLog. For example,
NSString *nameString = #"Name";
NSLog(#"nameString is: %#", nameString);
If my understanding is correct (which it very well may not be), then nameString is defined to be a pointer to a String. I thought then that this would print the memory address that nameString holds, not the value of that address. So, if that is true, then in the NSLog statement, to get the value of the pointer, shouldn't we need to use the asterisk notation to access what nameString points to like this:
NSLog(#"nameString is: %#", *nameString);
?
It has been a little while since programming in C, but since Objective-C is a superset of C I thought they would behave similarly.
An explanation would be greatly appreciated! Thanks!
The command %# is like "shortcut" that calls the method -description on the receiver. For an NSString it simply display the string itself, since is inherited from NSObject you can override it, very usefull if you create for own class. In that case the default behaviur is print the value of the pointer. If you want to print the address of the pointer in the string just replace with :
NSLog(#"nameString is: %p", nameString)
I think that you use an asterisk only to declare a pointer. Then, you only use the name you decided. For example:
NSString *foo = [[NSString alloc] initWithString:#"Hello"];
NSLog(#"%#", foo);
Correct me if I am wrong :)
It's an object and NSLog is a function that uses its format specifiers to determine what to do with the argument. In this case the specifier is %# which tells NSLog to call a method on an object.
Normally this will call the method "description" which returns an NSString but it probably does respondsToMethod first and falls through to some other string methods.
I don't want to use double pointer. I am using a function in simpler form as below.
-(NSString *) getName
{
return name;
}
So what is the correct way to take the returned NSString *?
By using #selector(getName) i am not able to get the returned value name.
Thank you in advance
You should use NSInvocation object instance for calling a selector and resolving returned result.
performSelector: does give you the return value directly.
NSString * s = #"NEXT WE HAVE NUMBER FOUR, 'CRUNCHY FROG'.";
NSString * l = [s performSelector:#selector(lowercaseString)];
NSLog(#"%#", l); // prints "next we have number four, 'crunchy frog'."
I am having issues with a category method used to percent-escape illegal symbols.
This is the code that i am using for the task:
#implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(encoding));
NSLog(#"S: %#, Self: %#", s, self);
return [s autorelease];
}
#end
When ever i run this method on a string without any of the symbols found in the above matching-string, the method runs fine and the same string is returned back to me.
For instance if i have a string like #"test" it will output:
S: test, Self: test
But if i instead use a string like #"test&symbols" it will output:
S: null, Self: test&symbols
Hence something seems to be wrong with the use of CFURLCreateStringByAddingPercentEscapes.
Now i want to escape symbols such as & because they can occur in strings used as values in a query string, which would cause the query string to be misinterpreted.
Any idea's about what may be the issue here?
Thank you in advance! / Magnus
After finding out my big mistake.
I was told to answer the question to my self.
What i did wrong that i didn't pass NSStringEncoding value to the method, like NSUTF8StringEncoding but instead of that I was passing a CF value such as kCFStringEncodingUTF8.
The value is passed thru a converter to make it CF value which caused an error and it was already had the correct type.
Sorry for any inconvenience.
-Magnus
I am having an issue that I can't figure out.
I'm trying to run the rangeOfString method on a string, and I'm not sure how to determine if the string was not found. For example:
NSRange range = [#"abc" rangeOfString:#"d" options:NSCaseInsensitiveSearch range:NSMakeRange(0,3)];
Clearly, "d" is not contained in the string "abc." I'd like to be able to do this:
if(the range is empty since "d" is not in "abc")
//do something
What is the code for this?
Thanks!!
From the documentation of NSString
-[NSString rangeOfString]
Return Value
An NSRange structure giving the
location and length in the receiver of
the first occurrence of aString.
Returns {NSNotFound, 0} if aString is
not found or is empty (#"").
So it looks like:
if ([#"abc" rangeOfString:#"d"].location == NSNotFound){
//Do something
Is the Apple-approved way.
EDIT:
I made a really bad typo, fixed it, thanks Kalle.
Check the length of the range. If it's non-zero, it was found.