I want to save special characters/german/swedish character in NSDictionary and have to post this data to server, but the data saved in the dictionary is converted to some other format as in console output. I am trying to save this string as different typecasts but not getting.
As NSDictionary's data type is generic, and while sending to POST its sent as in the modified format, I want to save this data in NSDictionary as it is, so that it can be sent in proper format to server and readable at server-end
My code is
NSString *playerName = #"Lëÿlã Råd Sölvê"; // dummy player name
NSLog(#"playerName: %#",playerName);
NSDictionary *postParameters = #{#"playerName1": playerName,
#"playerName2": [NSString stringWithString:playerName],
#"playerName3": [NSString stringWithUTF8String:[playerName UTF8String]],
#"playerName4": [NSString stringWithCString:[playerName UTF8String] encoding:NSASCIIStringEncoding],
#"playerName5": [[NSString alloc] initWithString:playerName],
#"playerName6": [NSString stringWithFormat:#"%#",playerName]};
NSLog(#"postParameters: %#",postParameters);
and output is
playerName: Lëÿlã Råd Sölvê
postParameters: {
playerName1 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName2 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName3 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName4 = "L\U00c3\U00ab\U00c3\U00bfl\U00c3\U00a3 R\U00c3\U00a5d S\U00c3\U00b6lv\U00c3\U00aa";
playerName5 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName6 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
}
How can I achieve this...
There is nothing wrong with your code.
What you are seeing is an artefact of NSLog and the description method - the former invokes the latter to obtain the textual representation of an object for output. For NSString the string is displayed using Unicode. However for NSDictionary contained strings are displayed using Objective-C Unicode character escape sequences, which have the form '\Uxxxx'.
To assure yourself all is OK you can use:
for (NSString *key in postParameters)
NSLog(#"%# -> %#", key, postParameters[key]);
and everything should display fine (except playerName4 where you mess the string up yourself).
Related
I am working on QR Codes where where i will get some contents with multiple fields (ex. name,address,contact number etc) from QR Code.
When I tried I am getting response.
But in single string format.
So if I want to separate these contents.
How do I can separate the contents in different fields.
response :
VERSION:3.0
N:Patil;Pradumna
FN:Pradumna Patil
ORG:techsanskar
TITLE:iphone
ADR:;;;;;;
TEL;WORK;VOICE:
TEL;CELL:9420256819
TEL;FAX:
EMAIL;WORK;INTERNET:
URL:
BDAY:
END:VCARD
Thank you.
If you have string in this format name,address,contact number
Like these are seperated by ',' You can use
NSArray *items = [theString componentsSeparatedByString:#","];
NSString *name = items[0];
NSString *address = items[1];//etc
This is part of an incoming array:
variantArray: (
(
{
CardinalDirection = "North-West";
DirectionVariantId = "DcCi_1445_171_0_0";
Distance = "2.516606318971459";
RouteName = "Woodsy";
Shape = {
Points = (
{
I want to get the value of DirectionVariantId
I would normally loop and use
NSMutableArray *myString = [variantArray[i] valueForKey:#"DirectionVariantId"];
This isn't working and results in an exception when I try to examine the last character in the string:
NSString *lastChar = [myString substringFromIndex:[myString length] - 1];
This is a new data set for me and I'm missing something..
Thanks for any tips.
Json contain two curly bracket means nested array.
Try:
NSString *myString=[[[variantArray objectAtIndex:0] objectAtIndex:0] objectForKey:#"DirectionVariantId"];
I think you're looking for [variantArray[i] objectForKey:#"DirectionVariantId"];
You'd need to convert the object within your incoming array (variantArray[i]) to a NSDictionary but it might already be judging by your original output.
i am trying to get a substring from a parent string. parent string is mac address of device available in a dictionary. But when i try to convert it it si giving me error as below "[_NSInlineData substringFromIndex:]: unrecognized selector sent to instance".
I just want to remove some of the chars from the parent string (here it is mac address like "<0001ui3 234563>" ) and get "ui3 234563".
// This is my nsdictionary.
advertisementdata = { kCBAdvDataIsConnectable = 1; kCBAdvDataLocalName = "test123"; kCBAdvDataManufacturerData = <00029r21 6y051rt2>; kCBAdvDataServiceUUIDs = ( FFF0 ); kCBAdvDataTxPowerLevel = 0; }
// I want 9r216y051rt from kCBAdvDataManufacturerData;
NSString *str2;
str2=[advertisementData objectForKey:#"kCBAdvDataManufacturerData"];
NSString *str3;
str3=[str2 substringFromIndex:5];
// here I do not get the required 12 character string from str2 string.
get NSInlinedata error for all NSString method.
Thanks,
Note that iOS CoreBlueTooth advertisementData is binary NSData:
https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/index.html#//apple_ref/doc/constant_group/Advertisement_Data_Retrieval_Keys
Documentation clearly states:
CBAdvertisementDataManufacturerDataKey
A NSData object containing the manufacturer data of a peripheral.
BTW, when you have a NSDictionary value type NSData*, you get the error by casting this NSData into a NSString*
// this is a bug!
NSString* str2=[advertisementData objectForKey:#"kCBAdvDataManufacturerData"];
To inspect the dictionary value try
NSData* advData = [advertisementData objectForKey:#"kCBAdvDataManufacturerData"];
Unfortunately, since this is binary, you need to interpret it according to the peripheral manufacturer data documentation. There is no easy way to transform this into a NSString*
Sounds like you have an NSData object, and you're trying to use an NSString method to access it. You can convert your NSData object using the callinitWithData: encoding:
Then apply the substringFromIndex call to your new NSString.
The point that you have NSInlineData and what you need is hexadecimal representation of data object.
First step should be to get a hexadecimal representation of data object's content in property list format.
NSData *datakCBAdvDataManufacturerData = [advertisementData valueForKey:#"kCBAdvDataManufacturerData"];
NSString *strmanufacturerData = datakCBAdvDataManufacturerData.description;
NSString *ChkStr = [strmanufacturerData substringWithRange:NSMakeRange(10, 8)];//depends upon your requirement.
NSLog(#"%#",ChkStr);
unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:ChkStr];
[scanner scanHexInt:&outVal];
NSLog(#"%#",outVal);
This outVal variable will contain value what you need.
I have pretty simple NSString
f.e
<scirpt attribute_1="x" attribute2="x" attribute3="x" ... attributeX="x"/>
I need to find specific parameter, let's say attribute2 and replace it's value,
I know the exact name of parameter f.e attribute2 but I don't know anything about it's value.
I guess it can be easily done by regexp, but I quite newbie on it.
In conclusion: I want to grab
attribute2="xxxx...xxx"
from incoming string
Note: I don't want to use some 3rd party libs to achieve that (it's temporary hack)
Any help is appreciated
I hacked it together with some string operations:
NSDictionary* valuesToReplace = #{#"attribute_1" : #"newValue1"};
NSString* sourceHtml = #"<scirpt attribute_1=\"x\" attribute2=\"x\" attribute3=\"x\" attributeX=\"x\" />";
NSArray* attributes = [sourceHtml componentsSeparatedByString:#" "];
for (NSString* pair in attributes)
{
NSArray* attributeValue = [pair componentsSeparatedByString:#"="];
if([attributeValue count] > 1) //to skip first "script" element
{
NSString* attr = [attributeValue firstObject]; //get attribute name
if([valuesToReplace objectForKey:attr]) //check if should be replaced
{
NSString* newPair = [NSString stringWithFormat:#"%#=\"%#\"", attr, [valuesToReplace objectForKey:attr]]; //create new string for that attribute
sourceHtml = [sourceHtml stringByReplacingOccurrencesOfString:pair withString:newPair]; //replace it in sourceHtml
}
}
}
It's really only when you want to hack it and you know the format :) Shoot a question if you have any.
you can try this.. https://github.com/mwaterfall/MWFeedParser
import "NSString+HTML.h" along with dependencies
And write like this...
simpletxt.text = [YourHTMLString stringByConvertingHTMLToPlainText];
Using the DynamoDBItemRequest in extracting the 'firstName' object, what I get is this:
{S: Will,N: (null),B: (null),SS: (
),NS: (
),BS: (
),}
I've tried extracting the first object but it keeps giving me an error. After a bit of digging I realised that DynamoDB does not return an NSString or an NSArray object. Anyone have any luck extracting the dataset?
Here's my code -
-(void)tap
{
DynamoDBGetItemRequest *getItemRequest = [[DynamoDBGetItemRequest new] autorelease];
DynamoDBAttributeValue *attributeValue = [[[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:#"%d", 1]] autorelease];
getItemRequest.tableName = TEST_TABLE_NAME;
getItemRequest.key = [NSMutableDictionary dictionaryWithObject:attributeValue forKey:TEST_TABLE_HASH_KEY];
DynamoDBGetItemResponse *getItemResponse = [[AmazonClientManager ddb] getItem:getItemRequest];
NSMutableDictionary *userPreferences = getItemResponse.item;
NSArray *abc = [userPreferences objectForKey:#"lastName"];
NSLog(#"%#", abc);
}
Here's my attempt at the code that keeps giving me error->
// NSString *abb = abc[1];
// NSLog(#"%#", abb);
In DynamoDB, an attribute can be of only one type (String, Number, Binary, StringSet, NumberSet, BinarySet). The object that you are printing out is of type "String", and has the content "Will". Dynamo would probably be clearer if it printed it like this:
{S:Will}
But instead it's just converting the AttributeValue to a String, and showing you all of the other (empty) values inside of it:
{S: Will,N: (null),B: (null),SS: ( ),NS: ( ),BS: ( ),}
Everything else is just noise; all that's important is that it's a String with the content "Will".