Three20 : TTTableImageItem and URL containing space and special characters - three20

I have a this code :
NSString *url = [NSString stringWithFormat:#"tt://classObj?title=%#", obl.title];
TTTableImageItem *cell = [TTTableImageItem
itemWithText:obl.title
imageURL:obl.thumbURL
URL:url];
If my obl.title contains special characters like é à ç ô or a simple space, it's not correctly mapped to my selector :
- (id)initWithFailDetails:(NSString *)title query:(NSDictionary*)query
Is there any way to pass parameters containing these type of characters? By escaping them of whatever solution it may exists?
Thanks.

use base64 encoding, (from memory url escaping won't work) OR
do not use URLs to pass such parameters but use instead TTURLAction -applyQuery +
- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query

Related

comparing a url containing utf-8 encoded string with a string

I have a url
"/complete/search?nolabels=t&client=chrome&ds=yt&q=%D1%8D%D1%82%D0%BE%20%D1%80%D1%8D%D0%BF%20%D0%B8%D0%BC%D0%BF%D1%80&hl=ru"
and a list of key words. I need to determine if the url contains any of those words.
I checked this string %D1%8D%D1%82%D0%BE%20%D1%80%D1%8D%D0%BF%20%D0%B8%D0%BC%D0%BF%D1%80
using
utf-8 encoder
thought I had to change % to \x for it to work.
I try using regular expressions for this
select "/complete/search?nolabels=t&client=chrome&ds=yt&q=%D1%8D%D1%82%D0%BE%20%D1%80%D1%8D%D0%BF%20%D0%B8%D0%BC%D0%BF%D1%80&hl=ru" rlike encode("это рэп импр", "UTF-8");
but it doesn't accept a binary as an argument and base64(encode("это рэп импр", "UTF-8")) doesn't return the type of string I need.
My question is, how do I turn this это рэп импр into this %D1%8D%D1%82%D0%BE%20%D1%80%D1%8D%D0%BF%20%D0%B8%D0%BC%D0%BF%D1%80?
Found this
SELECT reflect("java.net.URLDecoder", "decode", "/complete/search?nolabels=t&client=chrome&ds=yt&q=%D1%8D%D1%82%D0%BE%20%D1%80%D1%8D%D0%BF%20%D0%B8%D0%BC%D0%BF%D1%80&hl=ru", "UTF-8");

NSURL errors with caret ^ in string

Attempting to get data on the S&P500 (symbol: ^GSPC) from Yahoo Finance. In playgrounds and scripts, the presence of a caret (^) in the string passed to NSURL errors with "Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, sub code=0x0)". Xcode 6b6 and b7.
Works fine with other ticker symbols (AAPL, MSFT, etc).
Any suggestions for how to get this working?
let symbols:String = "^GSPC"
let financeURL:String = "http://finance.yahoo.com/d/quotes.csv?s=\(symbols)&f=sl1c6p2"
var financeNSURL: NSURL = NSURL(string: financeURL) // ERROR (see above)
let tickerNSData: NSData = NSData(contentsOfURL: financeNSURL)
var output:NSString = NSString(data:tickerNSData, encoding:NSUTF8StringEncoding)
It's crashing because Swift (at least in Xcode6-Beta7) doesn't support returning nil from an object initializer. From the release notes:
Swift does not support object initializers that fail by returning
null. (16480364)
Workaround: If there is a factory method, use it instead. Otherwise,
capture the result in an optional. For example:
let url: NSURL? = NSURL(string: "not a url")
So, to avoid a crash, declare your financeNSURL as NSURL? (as in the example from the docs above), or use NSURL.URLWithString() instead of init(string:).
However, the root of the problem is that you're not encoding your URL parameters correctly.
If you call stringByAddingPercentEncodingWithAllowedCharacters(...) on symbols it works:
let symbols:String = "^GSPC".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let financeURL:String = "http://finance.yahoo.com/d/quotes.csv?s=\(symbols)&f=sl1c6p2"
let financeNSURL: NSURL? = NSURL(string: financeURL)
if let url = financeNSURL {
let tickerNSData: NSData = NSData(contentsOfURL: url)
var output:NSString = NSString(data:tickerNSData, encoding:NSUTF8StringEncoding)
}
Output:
"^GSPC",1999.83,"-2.45","-0.12%"
When creating a NSURLwith init method NSURLWithString: the parameter URLString must be a properly encoded URL string. Your's is not. So, what is this a "properly encoded URL string"?
The corresponding official documentation gives a few more hints where to read:
"URLString: The URL string with which to initialize the NSURL object. This URL string must conform to URL format as described in RFC 2396, and must not be nil. This method parses URLString according to RFCs 1738 and 1808."
(links are mine).
So basically, you need to separately encode each URL component from your source string component using the correct variant of the percent encoding algorithm. Then, compose all encoded string components to the final URL string.
This can be tedious and is certainly error prone. Thus, since iOS 8 there is NSURLComponents (see docs here) which can aid you in this task.

iPhone SDK: Append NSString and Count Data

The web service that I am accessing returns this values:
A1;A2;A3;A4;A5;B2;B10;B11;B12;
I want to get the total count of all the data returned separated by ;. How can I do this? I'm thinking of doing a loop inside the string, get the value before ; but getting confused on how to properly run the loop.
There are many string functions and you can use one of them, that is componentsSeparatedByString.
Considering that above mentioned return value is a NSString.
NSString *strResponse = #"A1;A2;A3;A4;A5;B2;B10;B11;B12;";
NSArray *arCount = [strResponse componentsSeparatedByString:#";"];
NSLog(#"Total objects in response is: %d", [arCount count]);

NSPredicate raises 'Unknown number type or nil passed to arithmetic function expression.' in QuickLook plugin

I have an NSArray of NSDictionary. All the dictionaries have a key image-path.
I want to get a filtered array of only the dictionaries that match a given value (content of my path variable) for the key image-path.
I validated the structure of the data I have using this line (it prints me all the values of the dictionaries for the key image-path):
NSLog(#"array key paths: %#", [mountedImages valueForKeyPath:#"image-path"]);
I am using this code:
NSString *path = #"value-I-want-to-match";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(image-path LIKE[cd] \"%#\")", path];
NSArray *filteredArray = nil;
filteredArray = [mountedImages filteredArrayUsingPredicate:predicate];
The last line crashes and produces the following error:
[ERROR] Computing 6571367.19831142 raised 'Unknown number type or nil passed to arithmetic function expression.'
I am doing this in a QuickLook plugin. I can step-through my code with gdb, but I don't seem to get any trace. I only get:
[...]
[ERROR] Computing 6571367.19831142 raised 'Unknown number type or nil passed to arithmetic function expression.'
[Switching to process 23335 thread 0x8903]
[Switching to process 23335 thread 0xa703]
[Switching to process 23335 thread 0x8903]
Program ended with exit code: 0
You should remove the escaped quotes you placed in your predicate format string. As documented in the Apple predicate format string reference:
When string variables are substituted into a format string using %# , they are surrounded by
quotation marks. If you want to specify a dynamic property name, use %K in the format
string, as shown in the following example.
NSString *attributeName = #"firstName";
NSString *attributeValue = #"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K like %#", attributeName, attributeValue];
The predicate format string in this case evaluates to firstName like "Adam".
Single or double quoting variables (or substitution variable strings) cause %#, %K, or
$variable to be interpreted as a literal in the format string and so prevent any
substitution. In the following example, the predicate format string evaluates to firstName
like "%#" (note the single quotes around %#).
NSString *attributeName = #"firstName";
NSString *attributeValue = #"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K like '%#'", attributeName, attributeValue];
In your case, your predicate is being created as image-path LIKE[cd] "%#", which fails to evaluate correctly when used to filter your array.

predicateWithFormat vs stringWithFormat: is the former supposed to do the same as the latter?

I have an array with file names and I want to find all the names that end with e.g. 00001.trc when traceNum is 1. I tried this:
NSPredicate *tracePredicate = [NSPredicate predicateWithFormat:#"SELF ENDSWITH \"%05d.trc\"", traceNum];
and my predicate was SELF ENDSWITH "%05d.trc" instead of SELF ENDSWITH "00001.trc"
I tried this:
NSPredicate *tracePredicate = [NSPredicate predicateWithFormat:#"SELF ENDSWITH %#%05d.trc%#", #"\"", traceNum, #"\""];
and I got an exception: Unable to parse the format string "SELF ENDSWITH %#%05d.trc%#".
So I tried this:
NSPredicate *tracePredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"SELF ENDSWITH \"%05d.trc\"", traceNum]];
and it works.
So do I really need stringWithFormat in addition to predicateWithFormat or is there something I'm not doing correctly in creating my predicate?
You're correct; predicateWithFormat: is not quite the same as stringWithFormat:.
It's different for a couple major reasons:
It's not actually creating a new string. It's just looking through the format string and seeing what the next thing to substitute is, popping that off the va_list, and boxing it up into the appropriate NSExpression object.
It has to support a format specifier that NSString doesn't: %K. This is how you substitute in a key path. If you tried to substitute in the name of a property using %#, it would actually be interpreted as a literal string, and not as a property name.
Using formatting constraints (I'm not sure what the proper term is) like the 05 in %05d isn't supported. For one, it doesn't make sense. NSPredicate does numerical comparisons (in which case 00005 is the same thing as 5, and thus the zero padding is irrelevant) and string comparisons (in which you can just format the string yourself before giving it to NSPredicate). (It does other comparisons, like collection operations, but I'm skipping those for now)
So, how do you do what you're trying to do? The best way would be like this:
NSString *trace = [NSString stringWithFormat:#"%05d.trc", traceNum];
NSPredicate *p = [NSPredicate predicateWithFormat:#"SELF ENDSWITH %#", trace];
This way you can do all the formatting you want, but still use the more correct approach of passing a constant string as the predicate format.

Resources