I want to add string value dynamically from result.text and I wanted to display it in this way [#"17052648287",#"17052607335"] without losing the value. How can I do it?
NSMutableArray *strings = [#[#"17052648287",#"17052607335"] mutableCopy];
Add on coding
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result{
if (!result) return;
if(self.hasScannedResult == NO)
{
//Scan Result, added into array
NSString *scanPackage = [NSString stringWithFormat:#"%#", result.text];
scanLists = [NSMutableArray new];
[scanLists addObject:scanPackage];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSMutableArray *strings = [[NSMutableArray alloc]init];
strings = [#[result.text] mutableCopy];
[preferences setObject:strings forKey:#"strings"];
NSMutableArray *stringsArray = [preferences objectForKey:#"strings"];
for (NSString *string in stringsArray) {
NSLog(#"string: %#", string);
}
Declare an results array:
NSMutableArray * array = [NSMutableArray new];
Write below code where you get your result.text:
NSString *scanPackage = [NSString stringWithFormat:#"%#", result.text]; // If this code is working for you
[array addObject: scanPackage];
NSString *combined = [array componentsJoinedByString:#","];
NSLog(#"combined: %#", combined);
Related
here is my code. I used NSDictionary and coded to print my json data in my console.but i got error like this:
'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7c971930'
My code:
if(buttonIndex == 0) {
NSLog(#"OK Button is clicked");
}
else if(buttonIndex == 1) {
if([[textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]!=0)
{
if(!self.note)
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newNote = [NSEntityDescription insertNewObjectForEntityForName:#"Notes" inManagedObjectContext:context];
NSLog(#"%#",textView.text);
[newNote setValue:textView.text forKey:#"note"];
if([textView.text length]>30)
{
[newNote setValue:[NSString stringWithFormat:#"%#...",[textView.text substringToIndex:25]] forKey:#"title"];
}
else
[newNote setValue:textView.text forKey:#"title"];
[newNote setValue:[NSDate date] forKey:#"mod_time"];
//[newDevice setValue:self.versionTextField.text forKey:#"version"];
//[newDevice setValue:self.companyTextField.text forKey:#"company"];
How to overcome this problem to work and to print my data in my console
Help me out. I am struggling for 2 hours.I googled and change all change.But cant get data in my console. Thanks in advance
I guess you can get data like below this
NSDictionary *monday = jsonResults[#"original"];
NSArray * arrFile = monday[#"files"];
for (NSDictionary *theCourse in arrFile)
{
....
}
Did you checked that received data (i.e., returnData) from sendSynchronousRequest: is returning a plain data?
If the data received is in Base64, you might have to decode this NSData to plain data, and then go ahead with String conversion.
NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:responseData options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *str = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
// convert Json to NSDictionary
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
// NSLog(#"%#",jsonResults);
int count = [[jsonResults valueForKey:#"count"] intValue];
NSArray *arrayData = [jsonResults copy];
NSMutableArray *arrayPDFName = [[NSMutableArray alloc]init];
for(int i = 0;i < [arrayData count];i++)
{
NSDictionary *dictOriginal = [[arrayData objectAtIndex:2]valueForKey:#"original"];
int countOriginal = [[dictOriginal valueForKey:#"count"] intValue];
NSLog(#"The countOriginal is - %d",countOriginal);
NSArray *arrayFiles = [[dictOriginal valueForKey:#"files"] copy];
NSLog(#"The arrayFiles are - %#",arrayFiles);
for(int j=0;j<[arrayFiles count];j++)
{
NSString *strCreatedTime = [NSString stringWithFormat:#"%#",[[arrayFiles objectAtIndex:j] valueForKey:#"created_time"]];
NSString *strLastModifiedTime = [NSString stringWithFormat:#"%#",[[arrayFiles objectAtIndex:j] valueForKey:#"last_modified_time"]];
NSString *strID = [NSString stringWithFormat:#"%#",[[arrayFiles objectAtIndex:j] valueForKey:#"id"]];
NSString *strName = [NSString stringWithFormat:#"%#",[[arrayFiles objectAtIndex:j] valueForKey:#"name"]];
NSLog(#"The created_time is - %#",strCreatedTime);
NSLog(#"The last_modified_time is - %#",strLastModifiedTime);
NSLog(#"The is is - %#",strID);
NSLog(#"The name is - %#",strName);
[arrayPDFName addObject:strName];
}
}
When executing the below block. My friends_dict NSMutableDictionary value changes as friend changes. If I'm not wrong this is because the reference of the value in the dictionary is passed along to the friend. How can I pass the value instead of reference and prevent the dictionary from changing.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:#"friend_nearby"]){
NSString *id = [NSString stringWithFormat:#"%#",[object valueForKey:#"id"]];
NSString *spotValue = [NSString stringWithFormat:#"%#",[object valueForKey:#"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue] ) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}
Copy the array and pass in the copy.
NSArray *friendList = [friends_dict valueForKey:selectedFriendId];
NSMutableArray *friendsArray = [friendList mutableCopy];
I'm having a difficulty with this one I have a method that returns a value of Array of NSDictionary once I pass the return value of an array first it works. I already get the object in my NSLog but when I try to access the objects of my Array of NSDictionary in another method I get the error and I think that I loses the objects of the array.
Here's the method that return an Array of NSDictionary:
-(NSMutableArray *)selectItemDataTbl {
NSMutableArray *l_array_data = [[NSMutableArray alloc] init];
NSString *query = #"SELECT pid, fname, gen_name, type, fu, fprice FROM tbl_selectItem_data";
sqlite3_stmt *l_statement;
if (sqlite3_open([l_SqliteDb UTF8String], &(_database)) == SQLITE_OK) {
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &l_statement, nil) == SQLITE_OK) {
while(sqlite3_step(l_statement) == SQLITE_ROW){
NSMutableDictionary *l_dataDictionary = [[NSMutableDictionary alloc]init];
NSString *l_pid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 0)];
NSString *l_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 1)];
NSString *l_genName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 2)];
NSString *l_Type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 3)];
NSString *l_u = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 4)];
NSString *l_Price = [NSString stringWithUTF8String:(char *)sqlite3_column_text(l_statement, 5)];
[l_dataDictionary setObject:l_pid forKey:#"pid"];
[l_dataDictionary setObject:l_name forKey:#"name"];
[l_dataDictionary setObject:l_genName forKey:#"genName"];
[l_dataDictionary setObject:l_Type forKey:#"Type"];
[l_dataDictionary setObject:l_u forKey:#"u"];
[l_dataDictionary setObject:l_Price forKey:#"Price"];
[l_array_data addObject:l_dataDictionary];
}
NSLog(#"l_array_data count: %i", [l_array_data count]);
sqlite3_finalize(l_statement);
sqlite3_close(_database);
}
}
return l_array_data;
}
And here my method how I access it I'm calling it on my viewDidLoad:
- (void)selectItemData {
_l_alldataInSelectItem = [[NSMutableArray alloc] init];
_l_prodId = [[NSMutableArray alloc] init];
_l_prodName = [[NSMutableArray alloc] init];
_l_prodGenName = [[NSMutableArray alloc] init];
_l_prodCompType = [[NSMutableArray alloc] init];
_l_prodUom = [[NSMutableArray alloc] init];
_l_prodListPrice = [[NSMutableArray alloc] init];
_l_alldataInSelectItem = [dbc selectItemDataTbl];
_l_selectItemData_Dictionary = [[NSDictionary alloc] init];
for (_l_selectItemData_Dictionary in _l_alldataInSelectItem) {
NSString *Id = [_l_selectItemData_Dictionary valueForKey:#"pid"] ;
NSString *Name = [_l_selectItemData_Dictionary valueForKey:#"name"];
NSString *GenName = [_l_selectItemData_Dictionary valueForKey:#"genName"];
NSString *Type = [_l_selectItemData_Dictionary valueForKey:#"Type"];
NSString *U = [_l_selectItemData_Dictionary valueForKey:#"u"];
NSString *Price = [_l_selectItemData_Dictionary valueForKey:#"Price"];
[_l_prodId addObject:Id];
[_l_prodName addObject:Name];
[_l_prodGenName addObject:GenName];
[_l_prodCompType addObject:GenName];
[_l_prodUom addObject:U];
[_l_prodListPrice addObject:Price];
}
NSLog(#"adsfasdf %#", _l_prodId);
NSLog(#"adsfasdf %i", [_l_alldataInSelectItem count]);
}
But in this method I can't get the value of my array _l_alldataInSelectItem
- (void)textFieldshouldChange {
NSString *searchKey = self.txt_SearchField.text;
NSMutableArray *searchingData = [[NSMutableArray alloc] init];
if (![searchKey isEqualToString:#""]) {
NSLog(#"When TextField text is changed, this will be called");
NSLog(#"textFieldshouldChange _l_alldataInSelectItem: %i", [_l_alldataInSelectItem count]);
}
}
here's how i declare my l_alldataInSelectItem. I declared it on my header file
#property (strong, nonatomic) NSMutableArray *l_alldataInSelectItem, *l_prodId, *l_prodName, *l_prodGenName, *l_prodCompType, *l_prodUom, *l_prodListPrice;
Go systematically through the possibilities.
Possibility 1: There are gremlins somewhere trying to annoy you. We exclude that.
Possibility 2: You never created the data. Set a breakpoint on the return statement in selectItemDataTbl and check it.
Possibility 3: You never stored the array. Well, there is the fact that you store an empty mutable array into _l_alldataInSelectItem which is absolutely pointless, but set a breakpoint on the last statement in selectItemData and check it.
Possibility 4: You stored it and overwrote it. Search for all places where you store to _l_alldataInSelectItem or to a property named l_alldataInSelectItem, set breakpoints everywhere, and check.
Possibility 5 (the stupid one): _l_alldataInSelectItem is a weak instance variable or a weak property.
Possibility 6: You stored it, but looked for it in a different place. Is the self in textFieldshouldChange the same as the self in selectItemData? Set breakpoints and check.
Possibility 7: You are doing something asynchronously. The variable will be set, but much later than you think.
Most important: Put it very strongly in your mind that you did something wrong, and you don't have to solve any magical puzzles, all you need to do is find out what you did wrong.
I have an array of strings that I want to use as the filter for another array of dictionaries that is created from a plist. For example, if I had a plist of dictionaries that looked like so:
Key: Value:
car1 audi
car2 bmw
car3 bmw
car4 audi
car5 jaguar
and my array of strings was "audi, jaguar". How would I code it so that I can create a new array that would return "car1, car4, car5"? Hope this makes sense. Or better yet, how can I walk down this dictionary and filter it based on a value and then create a new array of dictionaries to use.
Code:
-(void)plotStationAnnotations {
desiredDepartments = [[NSMutableArray alloc] init];
BOOL tvfrSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"tvfrSwitchStatus"];
BOOL hfdSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"hfdSwitchStatus"];
if (tvfrSwitchStatus) {
NSString *tvfr = #"TVF&R";
[desiredDepartments addObject:tvfr];
}
if (hfdSwitchStatus) {
NSString *hfd = #"HFD";
[desiredDepartments addObject:hfd];
}
NSLog(#"Array 1 = %#", desiredDepartments);
NSString *path = [[NSBundle mainBundle] pathForResource:#"stationAnnotations" ofType:#"plist"];
NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSMutableArray *newDictionaryArray = [NSMutableArray array];
for (NSDictionary *dictionary in anns) {
for (NSString *string in desiredDepartments) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}
NSLog(#"Array = %#", keyMutableArray);
for (int i = 0; i < [keyMutableArray count]; i++) {
float realLatitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"latitude"] floatValue];
float realLongitude = [[[keyMutableArray objectAtIndex:i] objectForKey:#"longitude"] floatValue];
StationAnnotations *myAnnotation = [[StationAnnotations alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[keyMutableArray objectAtIndex:i] objectForKey:#"station"];
myAnnotation.subtitle = [[keyMutableArray objectAtIndex:i] objectForKey:#"department"];
[mapView addAnnotation:myAnnotation];
}
}
May be something like
NSArray *array1;
if([array1 containsObject : someValue])
can help. someValue can be your values you want to check if they exist in array1.
You can do something like this to filter by keys:
NSArray *keysToLookFor = [NSArray arrayWithObjects:#"car1", #"car4", #"car5", nil];
NSArray *foundObjects = [dictionary objectsForKeys:keysToLookFor notFoundMarker:nil];
Or something like this to filter by values:
NSString *valueToLookFor = #"Audi";
NSArray *keyArray = [dictionary allKeysForObject:valueToLookFor];
// To filter by multiple values
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
NSMutableArray *keyMutableArray = [NSMutableArray array];
for (NSString *string in valuesToFilterBy) {
[keyMutableArray addObjectsFromArray:[dictionary allKeysForObject:string]];
}
Updated answer for dictionaries in arrays:
NSArray *dictionaryArray; // The array of dictionaries that you have
NSMutableArray *newDictionaryArray = [NSMutableArray array];
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:#"Bmw", #"Audi", nil];
for (NSDictionary *dictionary in dictionaryArray) {
for (NSString *string in valuesToFilterBy) {
if ([dictionary allKeysForObject:string]) {
[newDictionaryArray addObject:dictionary];
break;
}
}
}
I am trying to pull an LDAP "jpegPhoto" attribute from an openLDAP server using a iOS openLDAP framework. The framework pulls the data as a dictionary of NSStrings.
I need to convert the NSString of "jpegPhoto" (which also appears to be base64 encoded) to UIImage, with the end result being that I display the jpegPhoto as the user's image when they login.
More Info:
-(NSDictionary *)doQuery:(NSString *)query:(NSArray *)attrsToReturn {
...
while(attribute){
if ((vals = ldap_get_values_len(ld, entry, attribute))){
for(int i = 0; vals[i]; i++){
//Uncomment if you want to see all the values.
//NSLog(#"%s: %s", attribute, vals[i]->bv_val);
if ([resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] == nil){
[resultSet setObject:[NSArray arrayWithObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]] forKey:[NSString stringWithFormat:#"%s",attribute]];
}else{
NSMutableArray *array = [[resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] mutableCopy];
[array addObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]];
[resultSet setObject:array forKey:[NSString stringWithFormat:#"%s",attribute]];
}
}
ldap_value_free_len(vals);
};
ldap_memfree(attribute);
attribute = ldap_next_attribute(ld, entry, ber);
};
...
}
-(UIIMage *)getPhoto{
NSString *query = [NSString stringWithFormat:#"(uid=%#)",self.bindUsername];
NSArray *attrsToReturn = [NSArray arrayWithObjects:#"cn",#"jpegPhoto", nil];
NSDictionary *rs = [self doQuery:query:attrsToReturn];
NSString *photoString = [[rs objectForKey:#"jpegPhoto"] objectAtIndex:0];
NSLog(#"The photoString is: %i %#",[photoString length],#"characters long"); //returns 4
NSData *photoData = [NSData dataWithBase64EncodedString:photoString];
UIImage *userPhoto = [UIImage imageWithData:photoData];
return userPhoto;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.studentNameLabel.text = [NSString stringWithFormat:#"Hi %#!",[self.ldap getFullName]];
self.studentPhotoImage.image = [self.ldap getPhoto];
[self checkForProctor];
}
Try this code
NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage];
UIImage *beforeImage = [UIImage imageWithData:dataObj];
There are many similar questions in Stackoverflow.. Please refer the following links
UIImage to base64 String Encoding
UIImage from bytes held in NSString
(Since there has been no working code posted for getting the image data from LDAP, I wanted to add this answer for the benefit of future visitors.)
The missing piece was reading the binary data into an NSData object rather than an NSString when you have binary data that might contain NULL (zero) values within it, such as images or GUIDs.
value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];
+ (NSArray *)searchWithBaseDN:(const char *)baseDN andFilter:(const char *)filter andScope:(int)scope {
...
while(entry)
{
// create a dictionary to hold attributes
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
attribute = ldap_first_attribute(ld, entry, &ber);
while(attribute)
{
if ((vals = ldap_get_values_len(ld, entry, attribute)))
{
if (ldap_count_values_len(vals) > 1) {
NSMutableArray *values = [[NSMutableArray alloc] init];
for(int i = 0; vals[i]; i++) {
[values addObject:[NSString stringWithUTF8String:vals[i]->bv_val]];
}
[dictionary setObject:values forKey:[NSString stringWithUTF8String:attribute]];
} else {
NSObject *value = nil;
if (strcmp(attribute, "thumbnailPhoto") == 0 || strcmp(attribute, "objectGUID") == 0) {
value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];
} else {
value = [NSString stringWithFormat:#"%s", vals[0]->bv_val];
}
[dictionary setObject:value forKey:[NSString stringWithUTF8String:attribute]];
}
ldap_value_free_len(vals);
};
ldap_memfree(attribute);
attribute = ldap_next_attribute(ld, entry, ber);
};
...
}