ShareKit Api Change - iphone

I am trying to change ShareKit API code of attachment to this code below:
dialog.attachment = [NSString stringWithFormat:#"{\"name\":\"%#\",\"href\"
:\"%#\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL)
SHKEncode(item.title),SHKEncodeURL(item.URL)];
Xcode Keeps telling me prefix error.
What am I missing?

I'm sorry to say, but your code is one big piece of spaghetti code. It is hard to read, prone to mistakes and hard to maintain (not only for you, but for your fellow coders as well).
I'd like to suggest breaking down the code a bit - for example:
// one way to define constant strings...
NSString * const kSHKAttachmentTemplateString = #"{\"name\":\"%#\",\"href\":\"%#\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"
// and another one that uses a #defined constant string at the beginning of header file of .m file
#define SHK_ATTACHMENT_TEMPLATE_STR #"{\"name\":\"%#\",\"href\":\"%#\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"
// assuming item is an valid object
NSString *itemTitleOrURL = (item.title == nil) ? SHKEncodeURL(item.URL) : SHKEncode(item.title);
dialog.attachment = [NSString stringWithFormat:SHK_ATTACHMENT_TEMPLATE_STR, itemTitleOrURL, SHKEncodeURL(item.URL)];
Following a readable code style will definitely save you time while solving similar syntax errors.

You are missing an colon in the three-way conditional;
The original code looks like this:
dialog.attachment = [NSString stringWithFormat:#"{\"name\":\"%#\",\"href\"
:\"%#\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL)
SHKEncode(item.title),SHKEncodeURL(item.URL)];
Change it to this
dialog.attachment = [NSString stringWithFormat:#"{\"name\":\"%#\",\"href\"
:\"%#\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL):
SHKEncode(item.title),SHKEncodeURL(item.URL)];
The error is at the end of the third line.
You should have seen this in the XCode editor, with a little yellow charat under the position where the error is.

Related

Subscript and Superscripts in CDATA of an xml file. Using UILabel to display the parsed XML contents

I need to display subscripts and superscripts (only arabic numerals) within a UILabel. The data is taken from an XML file. Here is the snippet of XML file:
<text><![CDATA[Hello World X\u00B2 World Hello]]></text>
Its supposed to display X2 (2 as superscript). When I read the string from the NSXMLParser and display it in the UILabel, it displays it as X\u00B2. Any ideas on how to make it work?
I think you can do something like this, assuming the CDATA contents have been read into an NSString and passed into this function:
-(NSString *)removeUnicodeEscapes:(NSString *)stringWithUnicodeEscapes {
unichar codeValue;
NSMutableString *result = [stringWithUnicodeEscapes mutableCopy];
NSRange unicodeLocation = [result rangeOfString:#"\\u"];
while (unicodeLocation.location != NSNotFound) {
// Get the 4-character hex code
NSRange charCodeRange = NSMakeRange(unicodeLocation.location + 2, 4);
NSString *charCode = [result substringWithRange:charCodeRange];
[[NSScanner scannerWithString:charCode] scanHexInt:&codeValue];
// Convert it to an NSString and replace in original string
NSString *unicodeChar = [NSString stringWithFormat:%C", codeValue];
NSRange replacementRange = NSMakeRange(unicodeLocation.location, 6);
[result replaceCharactersInRange:replacementRange withString:unicodeChar];
unicodeLocation = [result rangeOfString:#"\\u"];
}
return result;
}
I haven't had a chance to try this out, but I think the basic approach would work
\u00B2 is not any sort of XML encoding for characters. Apparently your data source has defined their own encoding scheme (which, frankly, is pretty stupid as XML is capable of encoding these directly, using entities outside of CDATA blocks).
In any case, you'll have to write your own parser that handles \u#### and converts that to the correct character.
I asked the question to my colleague and he gave me a nice and simple workaround. Am describing it here, in case others also get stuck at this.
Firstly goto this link. It has a list of all subscripts and superscripts. For example, in my case, I clicked on "superscript 0". In the following HTML page detailing "superscript 0", goto "Java Data" section and copy the "⁰". You can either place this directly in XML or write a simple regex in obj-c to replace \u00B2 with "⁰". And you will get nice X⁰. Do the same fro anyother superscript or subscript that you might want to display.

Problem in return value on objective-C

please look at this code:
-(NSString *) myfun:(NSString *)name paramnoone:(int)a paramnotwo:(int)b {
static int numberofcall=0;
if(a>b) {
return name;
}
NSString *secondname = [[NSString alloc]init];
secondname = [name StringByAppendingString:#"test"];
numberofcall++;
return secondname;
}
i have a problem on it, when my code is on "return secondname" next step is going to "return name" on if statement part, im confusing a lot , because c++ does not have this problem,
please help me on solve it,
thanks for ur help and sorry for my bad English.
(Until the question is explained further I can't really answer the question, but still have valuable infos which justify being posted as an answer, so here it goes.)
In the line:
NSString *secondname = [[NSString alloc]init];
You allocate an empty string. But in the very next line:
secondname = [name StringByAppendingString:#"test"];
You overwrite the pointer secondname to the previously allocated empty string, thus creating a memory leak. Since you do not use the empty string at all, remove the first line and turn the second line into:
NSString *secondname = [name StringByAppendingString:#"test"];
Edit: Based on comments to the questions, I think what you're asking is this (correct me if I'm wrong):
You are debugging the method.
While stepping through the method with the debugger, the flow proceeds normally through the method.
But after the numberofcall++; line, the debugger suddenly jumps to the return name; instead of the return secondname; line.
If that's what's happening to you: this is normal behavior, unfortunately. When the debugger reaches a return statement the marker always "jumps" to the first return statement in the method. But even though it doesn't look that way, your return secondname; statement is really executed.

Objective-C: Comparing normal strings and strings found in NSMutableArrays

I am confused about strings (a beginner's problem, I'm afraid):
I have one NSMutableArray called Notebook. At index position 1, I have an object, which I think is a string. At least I put it into the array like this:
[NoteBook replaceObjectAtIndex:1 withObject:#"x-x-x-x"];
So far so good. If I put this into an UILabel, it will show x-x-x-x on my screen. The nightmare starts when I try to compare this string with other strings. Let's consider that I do not want to display the string x-x-x-x on my screen, but just to have a blank instead. So I thought I could achieve this by coding this:
NSString *tempDateString;
tempDateString = [NSString stringWithFormat:#"%#",[NoteBook objectAtIndex:1]];
if (tempDateString == #"x-x-x-x") {
UISampleLabel.text = #"";
}
For some reason, this does not work, i.e. even if the string at position 1 of my array is 'x-x-x-x', it will still not set my UISampleLabel to nothing.
I suppose that I am getting confused with the #"" markers. When do I really need them? Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing?
Any help and suggestions would be very much appreciated!
You need to compare string with isEqualToString:
if ([tempDateString isEqualToString:#"x-x-x-x"]) {
UISampleLabel.text = #"";
}
In addition to the question that's been answered:
Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing?
You can. Why do you think you can't?

if-query: if (Nslog isEqualtoString #"...") - How can I make this?

I want my app to do something when the last NSLog has a certain string. I thought I could realize this with an if-query and isEqualtoString, but how can I make this?
Sorry for my bad English ;)
Maybe I don't understand what you're trying to do, but you can just create the string somewhere, log it, and then test it:
NSInteger _someInt = 2;
NSString *_someString = #"bananas";
NSString *_stringToBeLogged = [NSString stringWithFormat:#"%d %#", _someInt, _someString];
NSLog(#"%#", _stringToBeLogged);
if ([_stringToBeLogged isEqualToString:#"2 bananas"]) {
NSLog(#"I logged two bananas...");
}
You could consider creating your own custom log function which calls NSLog() after checking for your string constant. This would keep your code a bit cleaner if you want this functionality in multiple places and also allows you to easily extend the logging function further if desired.

objective C string Confusion

I am having code
NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];
when i try to print NSLog(#"cell value is %#",cellValue1);
in log i am not getting anything,
if i use %s, i am getting some symbols, not the string located in cellValue1.
Please help me.
Thanks in advance.
Check to make sure that products1 is actually set. It sounds as though it's nil when you send it the -objectAtIndex: message.
It surely means your string is empty...
Check it with the length method...
Solution:
NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];
NSLog([NSString stringWithFormat:#"Cell Value is %#", cellValue1]);
Reason:
NSLog operates with String inputs. While your statement should work, if there is some/any issue with your original cellValue1 string, your original statement will not catch the issue and assure that that NSLog() is being handed a pure string. By using the stringWithFormat: syntax you assure that even if your cellValue1 values is null or nil, you will receive your "cell value is" comment and possible some hint as to what is being passed into the statement by your cellValue1 string.
Testing Note:
If the above doesn't work for you, Test your original string by just using NSLog(cellValue1);. If this doesn't work it will tell you that your original NSString is not properly pulling your product at indexPath.row values.
Hope this helps!