How to un-nest an array with dictionaries - nsarray

I dont understand how to "un-nest" this array with dictionaries..?
let path = NSBundle.mainBundle().pathForResource("List", ofType: "plist"
var array = NSArray(contentsOfFile: path) as ...
<array>
<dict>
<key>Title</key>
<string>Mickey Mouse</string>
<key>Image</key>
<string>mickeymouse.png</string>
</dict>
</array>

Your var array should be typed as:
var array : Array<Dictionary<String,String>> = ...
Having done that you will be able to access dictionary fields with:
array[0]["Image"]

Related

Strings in init are not localized

I do localization of my iOS SwiftUI app and using XCode Product/Export localizations.. function to create xcloc file. Everything works fine but strings in init() are not detected in shown in xcloc. Is there any way how to add them?
Thanks
You should replace all relevant String types that are initialized with static string literals with LocalizedStringKey, mainly in the funcs and init()s – so for example:
struct MyStruct {
var myVar: LocalizedStringKey
init(var: LocalizedStringKey) {
self.myVar = var
}
...
}
Stringtypes in model data have to stay untouched.
If there are missing cases left you can add them manually to your .strings file by
"original text" = "localized text";

How to make a list of variables in flutter?

I want to create a list in flutter that contains variable. Now when I am trying to edit the contents of the list, it actually gets stored in the list itself. For eg.,
static var _ratingSelected = "";
static var _disconnectSelected = "";
static var _uTypeSelected = "";
List finalSelectedList = [_uTypeSelected,_disconnectSelected,_ratingSelected];
This is my list, when I will edit the list, like,
finalSelectedList[0] = "Main";
The output list will be like
finalSelectedList = ["Main",_disconnectSelected,_ratingSelected];
It will edit the finalSelectedList[0] position of the list but not the variable _uTypeSelected. Is there any way so that the value gets stored in the variable _uTypeSelected and not at the position finalSelectedList[0]?
There is no way to do that. If you use finalSelectedList[0].someProp = "Main"; where the variables are instances of some classes that have a someProp property, then it would work, because there is an additional abstraction. Strings are primitive values and copied by value when added to a list and therefore there is no connection left between list entry and variable.
class Foo {
Foo(this.someProp);
String someProp;
}
static var _ratingSelected = Foo("");
static var _disconnectSelected = Foo("");
static var _uTypeSelected = Foo("");
List finalSelectedList = [_uTypeSelected,_disconnectSelected,_ratingSelected];
finalSelectedList[0].someProp = "Main";

Call C++ dll with struct containing char array from Node.js

I'm using the Node.js ffi addon to call a C++ DLL.
The problem I'm having is with the struct I'm supplying - it contains a char array - I don't believe I'm setting this up correctly.
As a result I am unable to access the contents.
Routine's definition from C++ header file:
int GetSysConfig(MyConfig * config);
The MyConfig struct is defined in C++ as follows:
typedef struct{
int attribute;
char path[256];
}MyConfig;
My corresponding Node.js struct definition:
var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');
var ArrayType = require('ref-array');
// This seems to be the problematic part?
var charArray = ArrayType('char');
charArray.length = 256;
var MyConfig = StructType({
'attribute' : 'int',
'path' : charArray
})
Note: Below here is where I call the DLL from Node.js - I don't think there's a problem here although I could be wrong.
// Create a pointer to the config - we know we expect to supply this to the C++ routine.
var myConfigPtr = ref.refType(MyConfig);
var lib = ffi.Library('my.dll', {
"GetSysConfig": ["int", [myConfigPtr]]
});
var myConfigObj = new MyConfig();
lib.GetSysConfig.async(myConfigObj.ref(), function(err, res) {
console.log("attribute: " + myConfigObj.attribute);
// This is always empty [] - when it shouldn't be.
console.log("path: " + JSON.Stringify(myConfigObj.path));
});
Does anyone know where I'm going wrong with this?
For structs containing arrays: These should be defined with their size specified as a parameter to ArrayType.
For example:
ArrayType('char', 256)
Therefore the fix for my problem is the following:
var MyConfig = StructType({
'attribute' : 'int',
'path' : ArrayType('char', 256)
})

Why does this cfscript function work?

I wrote this function, the last line seems wrong* but it actually works. Can someone explain how does this stuff work ?
function convertEncoding(str,from,to) {
var charSetObj = createobject("java", "java.nio.charset.Charset");
var e_to = charsetObj.forName(from);
var e_from = charsetObj.forName(to);
return e_from.decode(e_to.encode(str)).toString();
}
I am on BlueDragon 7 and 7.1JX (not the open source)
I was inspired from this function : http://acoderslife.com/index.cfm/blog/Converting-Text-From-UTF-8-to-ISO-8859-1
* It seems that our last action is to work with the From encoding. It should be From.decode(string) and then To.encode(decoded_string)
The reason it seems off is that you swapped the variable names, so they do not accurately represent the contents:
var e_to = charsetObj.forName(from); // Original encoding
var e_from = charsetObj.forName(to); // New encoding
The reason it works is because the final statement accounts for this by swapping the variables positions, so that despite their names, the code is actually doing this:
return newEncoding.decode( originalEncoding.encode(str) ).toString();
Obviously best to fix the variable names, so you are not scratching your head when you run across this code six months from now.
function convertEncoding(str, from, to) {
var charSetObj = createobject("java", "java.nio.charset.Charset");
var origEncoding = charsetObj.forName( arguments.from );
var newEncoding = charsetObj.forName( arguments.to );
return newEncoding.decode(origEncoding.encode( arguments.str )).toString();
}

flex match regex

I have asked this question before and got some reponses but i am still not cleared on how to do this, if someone could help me more i would appreciate.
What i am doing is reading some information from a URL then i am using a regex(match) to get the string that i need. However i need the result for each match to be a string because i need to manipulate this string. below is the code tha t i have. Any help is appeciated.Again i have asked this but i am not sure how to proceed on my side.
var composer:StandardFlowComposer = txtSource.textFlow.flowComposer as StandardFlowComposer;
var pattern:RegExp= new RegExp("href=\"/player/.*?\">");
var pattern2:RegExp= new RegExp("href=\"/player/.*?\"");
var myArrayOfLines:Array = ul.data.split(/\n/);
var line:Array;
var str:String = "";
var lineFinal:Array;
var lineURL:String;
lineFinal = myArrayOfLines;
for each (var lineRaw:String in myArrayOfLines)
{
trace(lineRaw);
}
trace(line);
Anymore help is appreciated. thank you very much.
Try this , this will matches the specifed pattern against the string and returns array of strings...
var myArrayOfLines:Array = ul.data.match(/"your pattern here"/gs);
for each (var lineRaw:String in myArrayOfLines)
{
trace(lineRaw);
}
trace(line);
Try your regex here