Error : -[NSURL rangeOfString:]: unrecognized selector sent to instance 0x6180000a0de0 - objective-c

I need help identifying my error. Does anyone know why it errors?
if (result == NSFileHandlingPanelOKButton) {
NSString *filePath = [[openPanel URLs] objectAtIndex:0];
NSLog(#"%#", filePath);
*CODE WORKS UP TO HERE*
NSString *strTemp = [self extractString:filePath toLookFor:#"//" skipForwardX:2 toStopBefore:#".png"];
NSLog(#"%#",strTemp);
NSString *copyScript = [NSString stringWithFormat:#"%# /tmp/%#.png", strTemp, myString];
strTemp = [[NSString alloc] init];
NSString *path = #"/Applications/APP.app/Contents/Resources/copy.sh";
NSArray *args = [NSArray arrayWithObjects:copyScript, nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];
NSString* linkName = #"/tmp/";
NSString* extension = #".png";
NSString* fullPath = [NSString stringWithFormat:#"%#%#%#", linkName, myString, extension];
urlPathOfFile = [NSString stringWithFormat:#"URL/%#%#", myString, extension];
fileURL = [NSURL URLWithString:fullPath];
action = upload;
[self runAction];
Is it a memory error because I have too many strings?

There's a high probability that filePath points to a NSURL object, not NSString.

Related

FatSecret API "invalid signature"

Using this repository I was not able to make the queries work when oauth_token must be provided. I always get invalid signature. Tried a lot of solutions and tweaks in the code, nothing worked. Please help.
This is the code from the git mentioned:
NSString *OAuthorizationHeader(NSURL *url, NSString *method, NSData *body, NSString *_oAuthConsumerKey, NSString *_oAuthConsumerSecret, NSString *_oAuthToken, NSString *_oAuthTokenSecret)
{
NSString *_oAuthNonce = [NSString ab_GUID];
NSString *_oAuthTimestamp = [NSString stringWithFormat:#"%d", (int)[[NSDate date] timeIntervalSince1970]];
NSString *_oAuthSignatureMethod = #"HMAC-SHA1";
NSString *_oAuthVersion = #"1.0";
NSMutableDictionary *oAuthAuthorizationParameters = [NSMutableDictionary dictionary];
[oAuthAuthorizationParameters setObject:_oAuthNonce forKey:#"oauth_nonce"];
[oAuthAuthorizationParameters setObject:_oAuthTimestamp forKey:#"oauth_timestamp"];
[oAuthAuthorizationParameters setObject:_oAuthSignatureMethod forKey:#"oauth_signature_method"];
[oAuthAuthorizationParameters setObject:_oAuthVersion forKey:#"oauth_version"];
[oAuthAuthorizationParameters setObject:_oAuthConsumerKey forKey:#"oauth_consumer_key"];
if(_oAuthToken)
[oAuthAuthorizationParameters setObject:_oAuthToken forKey:#"oauth_token"];
// get query and body parameters
NSDictionary *additionalQueryParameters = [NSURL ab_parseURLQueryString:[url query]];
NSDictionary *additionalBodyParameters = nil;
if(body) {
NSString *string = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
if(string) {
additionalBodyParameters = [NSURL ab_parseURLQueryString:string];
}
}
// combine all parameters
NSMutableDictionary *parameters = [oAuthAuthorizationParameters mutableCopy];
if(additionalQueryParameters) [parameters addEntriesFromDictionary:additionalQueryParameters];
if(additionalBodyParameters) [parameters addEntriesFromDictionary:additionalBodyParameters];
// -> UTF-8 -> RFC3986
NSMutableDictionary *encodedParameters = [NSMutableDictionary dictionary];
for(NSString *key in parameters) {
NSString *value = [parameters objectForKey:key];
[encodedParameters setObject:[value ab_RFC3986EncodedString] forKey:[key ab_RFC3986EncodedString]];
}
NSArray *sortedKeys = [[encodedParameters allKeys] sortedArrayUsingFunction:SortParameter context:(__bridge void *)(encodedParameters)];
NSMutableArray *parameterArray = [NSMutableArray array];
for(NSString *key in sortedKeys) {
[parameterArray addObject:[NSString stringWithFormat:#"%#=%#", key, [encodedParameters objectForKey:key]]];
}
NSString *normalizedParameterString = [parameterArray componentsJoinedByString:#"&"];
NSLog(#"normalizedParameters: %#", normalizedParameterString);
NSString *normalizedURLString;
if ([url port] == nil) {
normalizedURLString = [NSString stringWithFormat:#"%#://%#%#", [url scheme], [url host], [url path]];
} else {
normalizedURLString = [NSString stringWithFormat:#"%#://%#:%#%#", [url scheme], [url host], [url port], [url path]];
}
NSString *signatureBaseString = [NSString stringWithFormat:#"%#&%#&%#",
[method ab_RFC3986EncodedString],
[normalizedURLString ab_RFC3986EncodedString],
[normalizedParameterString ab_RFC3986EncodedString]];
NSLog(#"signature base: %#", signatureBaseString);
NSString *key = [NSString stringWithFormat:#"%#&%#&",
[_oAuthConsumerSecret ab_RFC3986EncodedString],
[_oAuthTokenSecret ab_RFC3986EncodedString]];
NSLog(#"key codes: %#", key);
NSData *signature = HMAC_SHA1(signatureBaseString, key);
NSString *base64Signature = [signature base64EncodedString];
// PARKER CHANGE: changed oAuthAuthorizationParameters to parameters
NSMutableDictionary *authorizationHeaderDictionary = [parameters mutableCopy];
[authorizationHeaderDictionary setObject:base64Signature forKey:#"oauth_signature"];
NSMutableArray *authorizationHeaderItems = [NSMutableArray array];
for(NSString *key in authorizationHeaderDictionary) {
NSString *value = [authorizationHeaderDictionary objectForKey:key];
NSLog(#"KEY: %#", key);
NSLog(#"VALUE: %#", value);
// PARKER CHANGE: removed quotes that surrounded each value
[authorizationHeaderItems addObject:[NSString stringWithFormat:#"%#=%#",
[key ab_RFC3986EncodedString],
[value ab_RFC3986EncodedString]]];
}
// PARKER CHANGE: changed concatentation string from ", " to "&"
NSString *authorizationHeaderString = [authorizationHeaderItems componentsJoinedByString:#"&"];
// authorizationHeaderString = [NSString stringWithFormat:#"OAuth %#", authorizationHeaderString];
NSLog(#"final: %#", authorizationHeaderString);
return authorizationHeaderString;
}
And this is how I'm calling it:
- (void) makeUserRequestWithMethod:(NSString *)method
parameters:(NSDictionary *)params
completion:(void (^)(NSDictionary *data))completionBlock {
NSLog(#"%s", __func__);
NSMutableDictionary *parameters = [params mutableCopy];
[parameters addEntriesFromDictionary:[self defaultParameters]];
[parameters addEntriesFromDictionary:#{ #"method" : method }];
NSString *queryString = [self queryStringFromDictionary:parameters];
NSData *data = [NSData dataWithBytes:[queryString UTF8String] length:queryString.length];
NSString *authHeader = OAuthorizationHeader([NSURL URLWithString:FAT_SECRET_API_ENDPOINT],
#"POST",
data,
_oauthConsumerKey,
_oauthConsumerSecret,
_oAuthToken,
_oAuthTokenSecret
);
NSLog(#"header: %#", authHeader);
NSURL *url = [NSURL URLWithString:[FAT_SECRET_API_ENDPOINT stringByAppendingFormat:#"?%#", authHeader]];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
id JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
completionBlock(JSON);
} else {
completionBlock(nil);
}
}] resume];
}
I'm going to go out on a limb here and say that the code that others have proven to work is probably not at fault here, but rather first look at your own code. You say the error messages is "Invalid Signature" which sounds like it's a warning from the server you're calling, not from the code you're using.
[parameters addEntriesFromDictionary:[self defaultParameters]];
[parameters addEntriesFromDictionary:#{ #"method" : method }];
NSString *queryString = [self queryStringFromDictionary:parameters];
What happens inside [self defaultParameters] and are you 100% certain the parameters are appropriate (including spelling) for the API you're calling?
Have you verified that [self queryStringFromDictionary:parameters] prepares a properly formatting query string, and isn't introducing some error (non-escaped special characters, or percent encoding, for example)?

Error on displaying NSArray objectAtIndex into UILabel

Despite many tries I have already done.. I am not getting to display correctly items from an NSArray into a UILabel..
When I NSLog the array the console returns me this:
2013-01-19 14:34:32.799 bloom[2766:c07] (
"0.877"
)
Which is the value I fetched from a website and parsed and etc..
The problem is that when I display it in a UILabel it shows me exactly the same thing and I need just the value without quotes to be displayed. Here's is how it is displaying:
Edit:
Here is my code:
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
for (NSDictionary *valuesDatum in _detailItem) {
NSDictionary *itemAtIndex = (NSDictionary *)_detailItem;
self.title = [itemAtIndex objectForKey:#"SYMBOL"];
NSString *strUrl = #"http://www.bloomberg.com/quote/";
NSString *ativo = [itemAtIndex objectForKey:#"SYMBOL"];
NSString *consulta = [strUrl stringByAppendingString:ativo];
NSURL *url = [NSURL URLWithString:consulta];
NSData *webData = [NSData dataWithContentsOfURL:url];
NSString *xPathQuery = #"//span[#class=' price'] | //span[#class=' trending_up up'] | //span[#class=' trending_up up']/span | //table[#class='snapshot_table']/tr/td";
TFHpple *parser = [TFHpple hppleWithData:webData isXML:NO];
NSArray *array = [parser searchWithXPathQuery:xPathQuery];
valores = [[NSMutableArray alloc]init];
for (TFHppleElement *element in array) {
[valores addObject:[[element firstChild] content]];
}
novosValores = [[NSMutableArray alloc]init];
for (NSString *valuesDatum in valores) {
NSString *removeNewLine = [[valuesDatum componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#" "];
NSString *removeSpace = [removeNewLine stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *removeSpaceOne = [removeSpace stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *trocaVirgulaPonto = [removeSpaceOne stringByReplacingOccurrencesOfString:#"," withString:#"."];
[novosValores addObject:trocaVirgulaPonto];
}
valoresFinais = [[NSMutableArray alloc]init];
for (NSString *valuesDatum in novosValores) {
NSArray *val = [valuesDatum componentsSeparatedByString:#" - "];
[valoresFinais addObject:val];
}
infos = [[NSMutableArray alloc]init];
for (NSArray *dados in valoresFinais) {
NSArray *arrayDados = [[NSArray alloc]initWithArray:dados];
for (NSString *teste in arrayDados) {
NSArray *arrayTeste = [teste componentsSeparatedByString:#","];
[infos addObject:arrayTeste];
}
}
NSLog(#"%#",[infos objectAtIndex:0]);
NSString *fff = [[NSString alloc] initWithFormat:#"%#", [infos objectAtIndex:0]];
[_detailDescriptionLabel setText:fff];
}
}
}
NEW EDIT:
I have this array:
2013-01-19 15:43:05.055 bloom[3564:c07] (
"7.730",
"0.020",
"0.26%",
"7.750",
"7.650-7.800",
"2.333.100",
"7.710",
"3.730-8.810",
"+4.04%"
)
All I need is a new array with the data from lines 5 and 8 separated by the "-".
So anyone has a light??
Thanks!!!
I solved this question with the help of Anoop. I changed a bit his code and finally my code is like this:
for (NSDictionary *valuesDatum in _detailItem) {
NSDictionary *itemAtIndex = (NSDictionary *)_detailItem;
self.title = [itemAtIndex objectForKey:#"SYMBOL"];
NSString *strUrl = #"http://www.bloomberg.com/quote/";
NSString *ativo = [itemAtIndex objectForKey:#"SYMBOL"];
NSString *consulta = [strUrl stringByAppendingString:ativo];
NSURL *url = [NSURL URLWithString:consulta];
NSData *webData = [NSData dataWithContentsOfURL:url];
NSString *xPathQuery = #"//span[#class=' price'] | //span[#class=' trending_up up'] | //span[#class=' trending_up up']/span | //table[#class='snapshot_table']/tr/td";
TFHpple *parser = [TFHpple hppleWithData:webData isXML:NO];
NSArray *array = [parser searchWithXPathQuery:xPathQuery];
valores = [[NSMutableArray alloc]init];
for (TFHppleElement *element in array) {
[valores addObject:[[element firstChild] content]];
}
novosValores = [[NSMutableArray alloc]init];
for (NSString *valuesDatum in valores) {
NSString *removeNewLine = [[valuesDatum componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#" "];
NSString *removeSpace = [removeNewLine stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *removeSpaceOne = [removeSpace stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *removeSpaceTwo = [removeSpaceOne stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *removeDash = [removeSpaceTwo stringByReplacingOccurrencesOfString:#" - " withString:#" "];
NSString *trocaVirgulaPonto = [removeDash stringByReplacingOccurrencesOfString:#"," withString:#"."];
[novosValores addObject:trocaVirgulaPonto];
}
NSString *fullString=[novosValores componentsJoinedByString:#"_"];
NSString *changeDash = [fullString stringByReplacingOccurrencesOfString:#"-" withString:#"_"];
finalArray=[changeDash componentsSeparatedByString:#"_"];
//NSLog(#"%#", finalArray);
}
NSString *str = [[NSString alloc]initWithFormat:#"%#",[finalArray objectAtIndex:10]];
[_detailDescriptionLabel setText:str];
Are you sure you make like this:
label.text = [array objectAtIndex:0];
instead of:
label.text = array;
(
"0.877"
)
The above is an array, and you are putting that array onto label.
You have to use someLabel.text=[thatArray objectAtIndex:0];
EDIT:
As per your requirement try this one: (not compiler checked)
NSString *fullString=[array componentsJoinedByString:#"+"];
NSArray *brokenString=[fullString componentsSeparatedByString:#"-"];
NSString *mainString=brokenString[1];
NSArray *finalArray=[mainString componentsSepartedByString:#"+"];

JSON representation of NSDictionary

In my app a user can create UITextFields. to each field a tag is added, so that the tags correspond to the cases: 1, 2, 3, 4, ... then I add everything in a NSDictionary, and a json representation:
-(IBAction)buttonDropBoxuploadPressed:(id)sender{
//screenshot
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy_MM_dd"];
NSString *filename = [NSString stringWithFormat:#"By: %# ",
[formatter stringFromDate:[NSDate date]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#", filename] ];
//[data writeToFile:path atomically:YES];
//NSString *destDir = #"/sandbox/";
// [[self restClient] uploadFile:filename toPath:destDir
// withParentRev:nil fromPath:path];
// [[self restClient] loadMetadata:#"/sandbox/"];
//JSON
NSString *object;
NSString *object2;
NSString *object3;
NSString *object4;
NSString *object5;
NSString *object6;
NSString *object7;
NSString *object8;
NSString *object9;
NSString *object10;
NSString *object11;
NSString *object12;
NSString *object13;
NSString *object14;
NSString *object15;
for (UITextField *text in messagename) {
int touchedtag = text.tag;
NSUInteger tagCount = touchedtag;
switch (tagCount) {
case 1:
object = [NSString stringWithFormat:#"%#", text.text];
break;
case 2:
object2 = [NSString stringWithFormat:#"%#", text.text];
break;
case 3:
object3 = [NSString stringWithFormat:#"%#", text.text];
break;
case 4:
object4 = [NSString stringWithFormat:#" %#", text.text];
break;
case 5:
object5 = [NSString stringWithFormat:#"%#", text.text];
break;
case 6:
object6 = [NSString stringWithFormat:#"%#", text.text];
break;
case 7:
object7 = [NSString stringWithFormat:#"%#", text.text];
break;
case 8:
object8 = [NSString stringWithFormat:#"%#", text.text];
break;
case 9:
object9 = [NSString stringWithFormat:#"%#", text.text];
break;
case 10:
object10 = [NSString stringWithFormat:#"%#", text.text];
break;
case 11:
object11 = [NSString stringWithFormat:#"%#", text.text];
break;
case 12:
object12 = [NSString stringWithFormat:#"%#", text.text];
break;
case 13:
object13 = [NSString stringWithFormat:#"%#", text.text];
break;
case 14:
object14 = [NSString stringWithFormat:#"%#", text.text];
break;
case 15:
object15 = [NSString stringWithFormat:#"%#", text.text];
break;
default :
break;
}
}
//arrays
NSString * objects[] = { object, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13, object14, object15};
NSMutableArray *textnameobject = [[NSMutableArray alloc] initWithCapacity:b];
textnameobject = [NSMutableArray arrayWithObjects:objects count:b];
NSMutableArray *textnamekeys = [[NSMutableArray alloc] initWithCapacity:b];
NSString * textnumber[] = {#"title", #"title", #"title",#"title", #"title", #"title", #"title", #"title", #"title", #"title", #"title", #"title", #"title", #"title"};
textnamekeys = [NSMutableArray arrayWithObjects:textnumber count:b];
//arrays
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject: textnameobject forKey:textnamekeys];
/*
NSArray *objects2 = [NSArray arrayWithObjects:jsonDictionary, nil];
NSArray *keys2 = [NSArray arrayWithObjects:allkeys, nil];
NSDictionary *mainDict = [NSDictionary dictionaryWithObjects:objects2 forKeys:keys2];
*/
NSString* jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
[jsonData writeToFile:path atomically:YES];
NSString *destDir = #"/sandbox/";
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:path];
[[self restClient] loadMetadata:#"/sandbox/"];
//JSON
}
When I press the button I get the following error:
JSONRepresentation failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON object key must be string\" UserInfo=0x2e8370 {NSLocalizedDescription=JSON object key must be string}"
)
and consequentially a dropbox error. this worked on my previous app and the code is exactly the same. the json library is added correctly. I can't understand!! Please help!
Your code, rewritten.
- (IBAction)buttonDropBoxUploadPressed: (id)sender
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"yyyy_MM_dd"];
NSString *filename = [NSString stringWithFormat: #"By: %# ", [formatter stringFromDate: [NSDate date]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex: 0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: filename];
NSMutableDictionary *titles = [NSMutableDictionary dictionary];
for (UITextField *textField in messagename)
{
[titles setObject: textField.text forKey: #"title"];
// as you can see, here you're replacing the value # at key "title" with a new object on every pass
}
NSString *jsonString = [titles JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
[jsonData writeToFile: path atomically: YES];
NSString *destDir = #"/sandbox/";
[[self restClient] uploadFile: filename toPath: destDir withParentRev: nil fromPath: path];
[[self restClient] loadMetadata: #"/sandbox/"];
}
However, regarding my comment, you're not actually serializing your text fields' text into anything usable. At the end of this, at best, you'll have something that looks like this:
{
"title": "My Text Field Value"
}
Though I'm also relatively certain that one or more of your text fields' text is nil, which is causing your JSON problem.

Parsing a JSON text

With the following way...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"/lkj/"]];
NSString *fileName = [NSString stringWithFormat:#"/sandbox/2012_05_11.json"];
[[self restClient] loadFile:fileName intoPath:path];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:path];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from inside of "menu"
NSString *message = (NSString *) [data objectForKey:#"message"];
NSString *name = (NSString *) [data objectForKey:#"name"];
namegroup.text = [NSString stringWithFormat:#"%# %#",name, message];
...I am trying to parse a document I have previously made with other code...
{"message":["Untitled1a","Untitled2a","Untitled3a"],"name":["Untitled1b","Untitled2b","Untitled3b"]}
with the code above though, in name group.text, this appears...
(untitled, untitled, untitled) (untitled, untitled, untitled)
...but what I would like to do is to allocate many UITextFields, each of them in pairs, (2, 2, 2..), one field which displays the name and the other the message, so pair up 1a with 1b, 2a with 2b... obviously the fields won't be Untitled1a, but "how are you"...
But I can't seem to fix this issue!! Please help!!
You may try something like this:
NSArray *message = [data objectForKey:#"message"];
NSArray *name = [data objectForKey:#"name"];
NSDictionary* Dictionary = [NSDictionary dictionaryWithObjects:message forKeys:name];
for (NSString* Key in [Dictionary allKeys]){
NSLog(#"%# %#",Key,[Dictionary objectForKey:Key]);
}

stringWithContentsofurl generate leaks

im trying to get data via
NSString *string = [NSString
stringWithContentsOfURL: [NSURL
URLWithString: url]];
everything works fine, but when I run it with performance tool, it finds leaks on this line.
Here is whole mehod I use:
- (NSMutableDictionary *) getOutputImagesData: (URLParserImagesData) data
{
NSString *type = (data == URLParserImagesDataLatestImage) ? #"img" : #"size";
NSString *url = [NSString stringWithFormat: #"%s%s%s", [URLParserSiteURLforCategories UTF8String], [URLParserType UTF8String], [type UTF8String]];
//i get leaks here
NSString *string = [NSString stringWithContentsOfURL: [NSURL URLWithString: url]];
NSArray *imagesTemp = [string componentsSeparatedByString: #","];
NSMutableDictionary *outputImages = [NSMutableDictionary dictionary];
for(NSString *img in imagesTemp)
{
NSArray *splitStrings = [img componentsSeparatedByString: #"="];
if(data == URLParserImagesDataImagesCount)
{
NSNumber *integerValue = [NSNumber numberWithInt: [[splitStrings objectAtIndex:1] intValue]];
[outputImages setObject: integerValue forKey: [splitStrings objectAtIndex:0]];
}
else
[outputImages setObject: [splitStrings objectAtIndex:1] forKey: [splitStrings objectAtIndex:0]];
}
return outputImages;
}
There is nothing wrong in your code.
stringWithContentsOfURL: is deprecated. Try to use stringWithContentsOfURL:encoding:error: or stringWithContentsOfURL:usedEncoding:error: instead.