How do I send multiple querystring parameters in ios? - objective-c

Here is my code
Passing parameters to my Querystring gives me Bad Access Error!
NSString *myJson = #"http://mySite.com/Service.svc/MyList";
myJson = [myJson stringByAppendingFormat:#"?id=%#&uid=%#", firstId, secondId];
Can someone help me out!

What you do here is simple string formatting.
Given what you are doing, I guess firstId & secondId are integers, not objects, so your error is because you don't use the right format.
The format %# in you stringByAppendingFormat is for displaying an object, or more precisely the string returned by its description selector.
If you want to format an integer, just use %d as in C :)
This will give you :
NSString *myJson = #"http://mySite.com/Service.svc/MyList";
myJson = [myJson stringByAppendingFormat:#"?id=%d&uid=%d", firstId, secondId];

Related

How do I read the data from a TYPE_MIME_PART item?

It kinda works, but the problem is that it seems that the MIME_PART structure is not initialized ? all it's properties has the same values, even if I try to open a different mime item.
MIME_PART *pMime;
DHANDLE hPart;
char *pText;
WORD textLen;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pMime = OSLock(MIME_PART, hPart);
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
char *itemText = (char *)malloc(textLen);
memcpy(itemText, pText, textLen);
itemText[textLen] = '\0';
OSUnlock(hPart);
The itemText string has most of the content, but since the MIME_PART structure is not properly set, the pointer to the text is off...
So how do I properly set the MIME_PART?
Your code should do something like this instead:
DHANDLE hPart;
char *pchPart;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pchPart = OSLock(char, hPart);
In other words, lock the handle as type char instead of type MIME_PART. At this point, pchPart points to the beginning of the raw part data -- starting with a boundary (if present) and the headers. You can use NSFMimePartGetInfoByBLOCKID to get the length of the boundary and headers.
I realize this contradicts the documentation, but I've confirmed with a subject matter expert: The documentation is wrong.
Wrong answer, but the comments may be useful. My other answer is more correct.
This question could be improved. For example, you could show some sample data and describe the results when you try to read that data with your code.
But I'll try to answer based on the information I have. You calculated the text length like this:
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
That looks right to me, but then you do this:
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
Is wHeadersLen guaranteed to be equal to pMime->wHeadersLen? Also, you didn't consider the boundary length. Shouldn't you calculate the address like this instead?
pText = (char *)pMime + sizeof(MIME_PART) + pMime->wHeadersLen + pMime->wBoundaryLen;

NSJSONSerialization.JSONObjectWithData changes field type

I'm getting the following JSON response from the server:
{
"userId":"123456789",
"displayName":"display name"
}
When I use NSJSONSerialization.JSONObjectWithData and then prints the result NSDictionary I see in the console the following:
userId = 123456789
displayName = "display name"
Why do JSONObjectWithData changes the userId field type from String to a number?
It doesn't. The JSON deserialisation respects the data type and will maintain it. You can't tell the data type from a simple description log, you need to actually interrogate the class. The description log will quote some things if it makes more sense for the human reader, like spaces in the description, but it also omits quotes in some cases.
It doesn't.
Don't infer a variable type from its log representation, just test. Fire a Playground with this, for example:
let str = "{\"userId\":\"123456789\",\"displayName\":\"display name\"}"
if let data = str.dataUsingEncoding(NSUTF8StringEncoding),
jsonResult = try? NSJSONSerialization.JSONObjectWithData(data, options: []),
jsonObject = jsonResult as? [String:String],
id = jsonObject["userId"] {
print("User ID is " + id)
}

creating text from NSWindowsCP1252StringEncoding text

I have text below,
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\
\f0\fs24 \cf0 \'83}
In the above text \'83 corresponds to RTF file format and is in NSWindowsCP1252StringEncoding. Now my problem is how to convert to actual representation in
83 corresponds to string ƒ in NSWindowsCP1252StringEncoding.
NSString string with the encoding say using the api
stringWithCString:encoding. I have called the api like below.
NSString* str = [NSString stringWithCString:#"\'83" encoding:NSWindowsCP1252StringEncoding];
but it does not give the text. please let me know how to convert the value to particular text.
Regards,
Lenin
#"\'83" is not a c string, so you shouldn't be passing it to stringWithCString:encoding:. Even a C string version, "\'83", is just ascii characters so using the encoding NSWdindowsCP1252StringEncoding will not produce a string with any special characters.
Instead you need a c string with non-ascii values in order for NSWindowsCP1252StringEncoding to result in a non-ascii character like 'ƒ'.
NSString *str = [NSString stringWithCString:"\x83" encoding:NSWindowsCP1252StringEncoding];

Regex expression not working in objective c

//string
NSString *haystack = #".test {test:test} .test2{dasf:asdF}";
//pattern
NSString *needle = #"[^{}]*{[^{}]*}";
What I want from this is .test {test:test} and .test2{dasf:asdF} in an array (rest of the code handles this) but for some reason this regexp is not working correctly because no results are found.
If
NSString *needle = #"[^{}]*";
I get the following
(
".test ",
"",
"test:test",
"",
" .test2",
"",
"dasf:asdF",
"",
""
)
which is expected. After a lot of fiddling it seems to be a problem with { and } in the regex but I can't think why.
Incidently, if anyone can explain why I get these empty elements in the array above that would be useful to know as well.
Thanks!
Did you try [^{}]+ (with plus) instead of [^{}]*? Just because * matches also zero chars
Would you try using:
NSString *needle = #"[^{}]*\{[^{}]*\}";
indeed, { and } have special meaning in regexs (but they are sort of "automatically" escaped when used within [])

Convert quoted strings in a CSV to an NSMutableArray without the quotes

I have a question about the objective C. I have the following NSString *name shown below:
"First Name","Second Name","Last Name";
Actually, name is the header of the CSV and receive from the URL. And I use the follow statement to break the statement to array.
NSMutableArray *csvTitleArray;
csvTitleArray = [[name componentsSeparatedByString:#","];
The result of the array is
[0] = "First Name" // the " is part of the string, it means the first char of [0] is ", not F
[1] = "Second Name"
[2] = "Last Name"
However, I want to cancel the " in the begin and end of the string (the " is part of the string. Can anyone help me? Thank you.
Have a look at parsing csv data, the General CSV section (code example) handles your case.
See writing parser using nsscanner - csv for usefull more generic pointers about parsing data.
Have you thought of using stringByReplacingOccurrencesOfString:withString: and then splitting the string out into an array?