How to access individual values from an NSArray? - xamarin.ios

Trying to iterate through an array of values coming in from a push notification.
This is what the object in push notification object would look like:
"storyids": [12345, 12346, 12347, 12348]
At which, I can intercept this value in my ios app delegate, as such:
storyIds = aps.ObjectForKey(new NSString("storyids")) as NSArray;
But now I am having trouble iterating through this collection (which I want to essentially convert to a C# collection)
This is what I've tried:
for (nuint i = 0; i < storyIds.Count; i++)
{
var j = storyIds.ValueAt(i);
}
Which gives me a weird number and not the value at that particular index. I can't seem to find any method of NSArray to get the value from the array at a particular index.

You could use storyIds.GetItem<NSString> (0) or NSString.FromHandle (storyIds.ValueAt (0)).

Related

Netsuite Saved Search - Return a number randomly from given values

I am just wondering if someone could help me to return a number randomly from given values for a Netsuite Saved Search
For example:
I want to return either one of the 3 values randomly here: 196429,190569,150567
Thank you so much
First thing that needs to be done here is to return the saved search values and store them in an array. Please try this and let me know how this goes!!
1.Define an array like below
var getArray = [];
2.Push the saved search values to the array by looping through the saved search results like below (I am pushing internal id here, you can push any value you want)
for (var i = 0; i < searchResult.length; i++)
{
var id = searchResult[i].getValue({ name: 'internalid'});
getArray.push(id);
}
3.The last step would be to generate the random item from the array
var randomGen = getArray[Math.floor(Math.random()*getArray.length)];
log.debug('randomGen',randomGen);

NODE.JS: iterating over an array of objects, creating a new key if it does not exist

I am iterating over a collection of data, in my case, an array of objects. Here is a sample of 2 data points from it:
{
violation_id: '211315',
inspection_id: '268804',
violation_category: 'Garbage and Refuse',
violation_date: '2012-03-22 0:00',
violation_date_closed: '',
violation_type: 'Refuse Accumulation' },
{
violation_id: '214351',
inspection_id: '273183',
violation_category: 'Building Conditions',
violation_date: '2012-05-01 0:00',
violation_date_closed: '2012-04-17 0:00',
violation_type: 'Mold or Mildew' }
I need to create a new array of objects from this, one for each "violation_category" property. If Violation category already exists in the new array I am creating, i simply add the information to that existing category object (instead of having two "building conditions" objects for example, I would just add to an existing one).
However, I am having trouble assigning to the existing object if the current one exists (it's easy to check if it does not, but not the other way around). This is what am attempting to do currently:
if (violationCategory.uniqueCategoryName) {
violationCategory.uniqueCategoryName.violations = results[i].violation_id;
violationCategory.uniqueCategoryName.date = results[i].violation_date;
violationCategory.uniqueCategoryName.closed =
results[i].violation_date_closed;
} else {
category.violations = results[i].violation_id;
category.date = results[i].violation_date;
category.closed = results[i].violation_date_closed;
violationCategory.push(category);
}
In first condition, if this category (key) exists, I simply add to it, and in the second condition, this is where I am struggling. Any help appreciated. Thanks guys.
Just add an empty object to the key if there no object there :
violationCategory.uniqueCategoryName = violationCategory.uniqueCategoryName || {};
And only then, add the data you want to the object.
violationCategory.uniqueCategoryName.violations = results[i].violation_id;
violationCategory.uniqueCategoryName.date = results[i].violation_date;
violationCategory.uniqueCategoryName.closed =
results[i].violation_date_closed;
No condition needed.
Good luck!
Assuming that you have an input variable which is an array of objects, where the objects are looking like the objects of the question, you can generate your output like this:
var output = {};
for (var item of input) {
if (!output[item.violation_category]) output[item.violation_category] = [];
output[item.violation_category].push(item);
}
Of course you might customize it like you want.

How to get fields (attributes) out of a single CoreData record without using [index]?

I have one CoreData record that contains all of the app's settings. When I read that single record (using MagicalRecord), I get an array back. My question is: can I get addressabiltiy to the individual fields in the record without using "[0]" (field index), but rather using [#"shopOpens"]?
I was thinking something like this, but I don't think it's right:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"aMostRecentFlag == 1"]; // find old records
preferenceData = [PreferenceData MR_findAllWithPredicate:predicate inContext:defaultContext]; // source
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:#"preferencesDictionary"] mutableCopy]; // target
// start filling the userDefaults from the last Preferences record
/*
Printing description of preferencesDict: {
apptInterval = 15;
colorScheme = Saori;
servicesType = 1;
shopCloses = 2000;
shopOpens = 900;
showServices = 0;
syncToiCloud = 0;
timeFormat = 12;
}
*/
[preferencesDict setObject: preferenceData.colorScheme forKey:#"shopOpens"];
UPDATE
This is how I finally figured it out, for those who have a similar question:
NSPredicate *filter = [NSPredicate predicateWithFormat:#"aMostRecentFlag == 0"]; // find old records
NSFetchRequest *freqest = [PreferenceData MR_requestAllWithPredicate: filter];
[freqest setResultType: NSDictionaryResultType];
NSDictionary *perferenceData = [PreferenceData MR_executeFetchRequest:freqest];
Disclaimer: I've never used magical record, so the very first part is just an educated guess.
I imagine that preferenceData is an instance of NSArray firstly because the method name uses findAll which indicates that it will return multiple instances. Secondly, a normal core data fetch returns an array, and there is no obvious reason for that find method to return anything different. Thirdly, you referenced using an index operation in your question.
So, preferenceData is most likely an array of all objects in the store that match the specified predicate. You indicated that there is only one such object, which means you can just grab the first one.
PreferenceData *preferenceData = [[PreferenceData
MR_findAllWithPredicate:predicate inContext:defaultContext] firstObject];
Now, unless it is nil, you have the object from the core data store.
You should be able to reference it in any way you like to access its attributes.
Note, however, that you can fetch objects from core data as dictionary using NSDictionaryResultType, which may be a better alternative for you.
Also, you can send dictionaryWithValuesForKeys: to a managed object to get a dictionary of specific attributes.

Can I convert a JSON string to a Flex ArrayCollection in without changing the item order?

Regarding this issue, I have generated dynamically string from Java .each time string format change , for example String format is
[{"BranchName":"Corporate Office","Date":"08\/03\/2013","SPName":"Pharmacy","SPAmount_5-00%":"100.00","SPVATAmount_5-00%":"15.00","SPOtherCharges_5-00%":"30.00","SPAmount_14-50%":"200.00","SPVATAmount_14-50%":"39.00","SPOtherCharges_14-50%":"71.00","SPColTPA":"100.00","SPColChequeDD":"50.00","SPHdfcCC":"100.00","SPIdbiCC":"100.00","SPColCash":"50.00","Difference":"55.00"},
But when I convert array collection with following code .
var rawData:String = String(event.result);
var arr:Array = (JSON.decode(rawData) as Array);
var dp:ArrayCollection = new ArrayCollection(arr);
but array collection order changed as default sort like [Branch, Date, Difference,.. ] . But I want same as string format order. So How can I prevent Default order.
Actually, what you've described here is an array of objects (your example just includes one object). In the JSON parsing to array, each object is indeed in the order in which it is declared; but in the OBJECTS that are created, the properties may not be listed in the same order.
For example:
'[ {"Branch":"Corporate", "Department":"Finance", "Cost":"10000", "Attended":"true"},' +
'{"Branch":"Las Vegas", "Department":"Hospitality", "Cost":"20100", "Attended":"false"},' +
'{"Branch":"San Diego", "Department":"Banking", "Cost":"11023", "Attended":"true"}]'
Parsing would return arr[0] as the Corporate object, arr[1] as the Las Vegas object, etc. Iterating through the properties I got:
var obj:Object = dp.getItemAt(0);
for (var prop:String in obj) {
trace(prop + ' is ' + obj[prop]);
}
Department is Finance
Attended is true
Branch is Corporate
Cost is 10000

C#.net How to loop through objects within objects

Could someone help me. In C#.net I need to pull the properties and their values from objects within objects. Object "pc" may have other objects of different types in it and I need to recursively move through "pc" extracting what I need.
Picture of hierarchy found here:
http://www.virtualizeplanet.com/pcobjects.jpg
I've tried pass the val property into a separate object "cc" the tried to iterate through it with this code but I don't get the right results:
object cc = pc.val;
foreach (var pp in cc.GetType().GetProperties())
{
string name = pp.Name;
}
Could you solution be along these lines?
string name = PC.Name;
int[] values;
foreach (obj value in val)
{
values.Add(value);
}
// Do stuff

Resources