How do I cast an Array<AnyObject> to an empty array variable? - casting

Problem: I can't assign a string array (typed as 'AnyObject') to an empty array.
Steps:
1. Get a dictionary containing strings & arrays.
2. Grab an array from this dictionary via the key 'photos'.
Note: Xcode's warning suggest that I give the explicit type 'AnyObject'.
3. Create an empty array.
4. Attempt to assign it (failed).
let responseDictionary = responseDict as [String : AnyObject]
let ricPhotos:AnyObject = responseDictionary["photos"]!
var thePhotos:Array<AnyObject>?
thePhotos = ricPhotos <--- fails
Compiler Error:
...'AnyObject' is not convertible to 'Array<AnyObject>'
Question: How do I assign 'ricPhotos' to the empty array 'thePhotos' and preferably cast 'AnyObject' to 'String'?
Revision
let responseDictionary = responseDict as [String : AnyObject]
var anyObject: AnyObject? = responseDictionary["photos"]
Okay, 'anyObject' appears to be a Dictionary, and inside it is 'photo' which is an array; as seen in the data.
Here's some of the data(anyObject):
{
page = 1;
pages = 1334;
perpage = 100;
photo = (
{
farm = 3;
"height_m" = 336;
"height_s" = 161;
"height_sq" = 75;
"height_t" = 67;
id = 15166756998;
isfamily = 0;
isfriend = 0;
ispublic = 1;
owner = "127012961#N08";
secret = 053032f300;
server = 2941;
title = "You #NYPL";
"url_m" = "https://farm3.staticflickr.com/2941/15166756998_053032f300.jpg";
"url_s" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_m.jpg";
"url_sq" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_s.jpg";
"url_t" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_t.jpg";
"width_m" = 500;
"width_s" = 240;
"width_sq" = 75;
"width_t" = 100;
},
...etc.
I want to grab the 'photo' array. But I can't downcast 'anyObject' to 'Dictionary' so that I can subscript it. I tried:
var anyObject:Dictionary = responseDictionary["photos"]
But I got:
'(String, AnyObject)' is not convertible to '[String : AnyObject]'
So I'm stuck with:
var anyObject: AnyObject? = responseDictionary["photos"]
So with anyObject, I tried to access 'photos':
let RicPhotos = anyObject["Photo"] as [String : AnyObject]
...I also tried:
let RicPhotos = anyObject["Photo"] as Array<Dictionary<String,String>>
But I got:
'AnyObject?' does not have a member named 'subscript'
I can see the data, but I can't extract into an empty variable.
I attempted to downcast to a specific type (Dictionary) but the compiler refuses.
There must be a strait forward way of getting an embedded array from a dictionary whilst casting to its respective cast (without the 'anyObject').Any ideas?

Edited my answer to fit your edit...
let responseDictionary = [:] as [String : AnyObject]
var photosDic: AnyObject? = responseDictionary["photos"]
if let photosDic = photosDic as? Dictionary<String, AnyObject> {
var photosArray: AnyObject? = photosDic["photo"]
if let photosArray = photosArray as? Array<Dictionary<String, AnyObject>> {
//There you go
}
}

Related

Making a list using api in google sheets

I am attempting to pull faction information from torns api but it puts all data into a single cell rather than listing. heres what ive got so far.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Faction members')
.addItem('Pulls faction member info','callNumbers')
.addToUi();
}
function callNumbers() {
var response = UrlFetchApp.fetch("https://api.torn.com/faction/42911?selections=basic&key=xFtPCG2ygjbhmKWI");
Logger.log(response.getContentText());
var fact = response.getContentText();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,2).setValue([fact]);
}```
You could try to parse the information using JSON.parse. Something like:
function callNumbers() {
var response = UrlFetchApp.fetch("https://api.torn.com/faction/42911?selections=basic&key=xFtPCG2ygjbhmKWI");
Logger.log(response.getContentText());
var fact = response.getContentText();
var myObject = JSON.parse(fact);
// define an array of all the object keys
var headerRow = Object.keys(myObject);
// define an array of all the object values
var row = headerRow.map(function(key){ return myObject[key]});
// define the contents of the range
var contents = [
headerRow,
row
];
// select the range and set its values
var ss = SpreadsheetApp.getActive();
var rng = ss.getActiveSheet().getRange(1, 1, contents.length, headerRow.length )
rng.setValues(contents)
}

Cloud Kit how to re fetch records when error

Cloud WWDC says When you get a CKError.changeTokenExpired re-fetch changes by setting the previous server change token to nil. I tried something like this:
let operation = CKFetchRecordZoneChangesOperation()
operation.qualityOfService = .userInitiated
operation.recordZoneIDs = appDelegate.changedZoneIDs
let fetchOptions = CKFetchRecordZoneChangesOptions()
fetchOptions.previousServerChangeToken = nil
operation.optionsByRecordZoneID = [ recordZoneID : fetchOptions]
But the optionsByRecordZoneID has been deprecated. So how can I pass the previous server token to the server and continue fetching past the error handling?
You should use the instance property configurationsByRecordZoneID on your CKFetchRecordZoneChangesOperation().
How about:
let operation = CKFetchRecordZoneChangesOperation()
operation.qualityOfService = .userInitiated
operation.recordZoneIDs = appDelegate.changedZoneIDs
let fetchOptions = CKFetchRecordZoneChangesOperation.ZoneConfiguration()
fetchOptions.previousServerChangeToken = nil
var zoneConfiguration: [CKRecordZone.ID : CKFetchRecordZoneChangesOperation.ZoneConfiguration] = [:]
for zoneID in operation.recordZoneIDs {
zoneConfiguration[zoneID] = fetchOptions
}
operation.configurationsByRecordZoneID = zoneConfiguration

Can't get key value as String from metadata

I'm grabbing metadata from an MPMediaItem like this:
let url = item.value(forProperty:MPMediaItemPropertyAssetURL) as? NSURL
let asset = AVURLAsset(url: url! as URL, options: nil)
let metaArray = asset.metadata
for metadata in metaArray{
print("-----metadata:\(metadata)")
print("-----metadata.key:\(String(describing: metadata.key))")
}
However, when I get a block of metadata printed the "key" is printed as a numeric value instead of "pcst" as shown in the printout:
-----metadata:<AVMetadataItem: 0x1740153f0, identifier=itsk/pcst, keySpace=itsk, key class = __NSCFNumber, key=pcst, commonKey=(null), extendedLanguageTag=(null), dataType=com.apple.metadata.datatype.int8, time={INVALID}, duration={INVALID}, startDate=(null), extras={
dataLength = 1;
dataType = 21;
dataTypeNamespace = "com.apple.itunes";
}, value=1>
-----metadata.key:Optional(1885565812)
This is happening for all of the metadata/keys (there are 29 in this particular media item).
Also note that this line of code:
let realString = NSString(string: metadata.key! as! String)
causes this error:
Could not cast value of type '__NSCFNumber' (0x1b80dcdf0) to 'NSString' (0x1b80edae8).
How can I get the string value for the key ("pcst") ?
May be what you are looking for is identifier property of AVMetadataItem.
for metadata in metaArray{
print(metadata.identifier ?? "DefaultValue")
}
In case others want to see the code I'm using:
func returnKeyString(_ inVal: String)->String{
// expecting the metadata for "identifier" as input - returns key value
// eg "itsk/ldes" -> "ldes"
// or "id3/%00WFD" etc. -> "wfd"
var sFinal:String = ""
if (inVal.contains("/")){
sFinal = (inVal.components(separatedBy: "/")[1])
}
if sFinal.contains("%"){
sFinal = sFinal.components(separatedBy: "%")[1]
let index1 = sFinal.index(sFinal.startIndex, offsetBy: 2)
sFinal = sFinal.substring(from: index1)
}
return sFinal.lowercased()
}

How to add dynamic values to field injections list with custom trigger to camunda properties panel?

I have two questions here
Is it possible to add dynamic lists values to field injection list input ?
Can I create a trigger for this so this can be initiated from any other input selection say a class selection will populate all fields
I was just looking into FieldInjection.js whether that can be extented for the same
Can someone please provide a hint or direction for this ?
Thanks.
For anyone interested in the answer, I was able to achieve the above goal by changing the set function of the Java Class select input as folllowing
few imports
var extensionElementsHelper = require('../../../../helper/ExtensionElementsHelper'),
elementHelper = require('../../../../helper/ElementHelper')
var CAMUNDA_FIELD_EXTENSION_ELEMENT = 'camunda:Field';
function getExtensionFields(bo) {
return bo && extensionElementsHelper.getExtensionElements(bo, CAMUNDA_FIELD_EXTENSION_ELEMENT) || [];
}
then changing the set function to create extension element and push the field values as :
set: function(element, values, node) {
var bo = getBusinessObject(element);
var type = getImplementationType(element);
var attr = getAttribute(type);
var prop = {}
var commands = [];
prop[attr] = values.delegate || '';
var extensionElements = getExtensionFields(bo);
//remove any extension elements existing before
extensionElements.forEach(function(ele){
commands.push(extensionElementsHelper.removeEntry(getBusinessObject(element), element, ele));
});
if(prop[attr] !== ""){
var extensionElements = elementHelper.createElement('bpmn:ExtensionElements', { values: [] }, bo, bpmnFactory);
commands.push(cmdHelper.updateBusinessObject(element, bo, { extensionElements: extensionElements }));
var arrProperties = ["private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varOne","private org.camunda.bpm.engine.delegate.Expression com.cfe.extensions.SampleJavaDelegate.varTwo"]
var newFieldElem = "";
arrProperties.forEach(function(prop){
var eachProp = {
name:"",
string:"",
expression:""
}
var type = prop.split(" ")[1].split(".").reverse()[0];
var val = prop.split(" ")[2].split(".").reverse()[0];
eachProp.name = val;
if( type == "String"){
eachProp.string = "${" + val +" }"
}else if( type == "Expression"){
eachProp.expression = "${" + val +" }"
}
newFieldElem = elementHelper.createElement(CAMUNDA_FIELD_EXTENSION_ELEMENT, eachProp, extensionElements, bpmnFactory);
commands.push(cmdHelper.addElementsTolist(element, extensionElements, 'values', [ newFieldElem ]));
});
}
commands.push(cmdHelper.updateBusinessObject(element, bo, prop));
return commands;
}
Cheers !.

Getting ambiguous reference to member subscript

I am updating my code to swift3.0 but getting ambiguous refrence to member? What wrong i might be doing. Here is the method I am getting error in.
open class func parseJsonTenantList(_ list: [NSDictionary]?, strElementName: String, attrName1: String, attrNameValue2: String) -> [TenantRegister]
{
var renantList: [TenantRegister] = []
var key: String?
if let dict : [NSDictionary] = list {
var value: String?
for i in 0..<dict.count {
/// if attribute name doesn't match then it returns nil
if let s1: AnyObject = dict[i].value(forKey: attrName1)
{
key = s1 as? String
}
if let s2: AnyObject = dict[i].value(forKey: attrNameValue2)
{
value = s2 as? String
}
if (!(String.stringIsNilOrEmpty(value) && String.stringIsNilOrEmpty(key)))
{
let t: TenantRegister = TenantRegister()
t.name = key
t.tenantId = Guid(value!)
renantList.append(t)
}
}
}
return renantList
}
The issue is you are using NSDictionary, to solved your problem simply cast the list to Swift's native type [[String:Any]] and then use subscript with it instead of value(forKey:)
if let dict = list as? [[String:Any]] {
var value: String?
for i in 0..<dict.count {
/// if attribute name doesn't match then it returns nil
if let s1 = dict[i][attrName1] as? String
{
key = s1
}
if let s2 = dict[i][attrNameValue2] as? String
{
value = s2
}
if (!(String.stringIsNilOrEmpty(value) && String.stringIsNilOrEmpty(key)))
{
let t: TenantRegister = TenantRegister()
t.name = key
t.tenantId = Guid(value!)
renantList.append(t)
}
}
}
In Swift use native type Dictionary [:] and Array [] instead of NSDictionary and NSArray to overcome this type of issues.