Adding entries to Dictionary (not NSDictionary) from Info.plist - dictionary

In the following OS X application, how can I create a Dictionary in a similar way to how I create an NSDictionary?
I suppose I need to find a method on Dictionary that performs the same task as NSDictionary(contentsOfFile:infoPlist) but I am not sure what this Dictionary method is.
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification?) {
var infoPlist = "/Applications/Calendar.app/Contents/Info.plist"
var nsDictionary = NSDictionary(contentsOfFile:infoPlist)
println("nsDictionary = \(nsDictionary)")
var dictionary = Dictionary<String, String>()
// TODO: How do I add entries from "infoPlist" to "dictionary"
// like I do above with the old "nsDictionary"?
println("dictionary = \(dictionary)")
}
func applicationWillTerminate(aNotification: NSNotification?) {
}
}
Many thanks in advance.

Sadly there is no way to do this in native swift dictionaries at the moment. However using NSDictionary is still quite fine.
For more info you can look at this similar question

Related

How to implement TornadoFX WebEngine Callback in Kotlin

I am using Kotlin TornadoFX to create a browser. When I implement WebEngine setCreatePopupHandler, I get an error:
e: surfing\src\surfing.kt: (76, 13): Modifier 'override' is not
applicable to 'local function'
e: surfing\src\surfing.kt: (76, 13): Expected a value of type WebEngine!
I referenced this Java code using JavaFX:
webEngine.setCreatePopupHandler(
new Callback<PopupFeatures, WebEngine>() {
#Override
public WebEngine call(PopupFeatures config) {
smallView.setFontScale(0.8);
if (!toolBar.getChildren().contains(smallView)) {
toolBar.getChildren().add(smallView);
}
return smallView.getEngine();
}
});
Translated into Kotlin to use TornadoFX:
var wv = webview()
val br = wv.getEngine()
br.setCreatePopupHandler(Callback<PopupFeatures, WebEngine>() {
override fun call(pf: PopupFeatures): WebEngine {
var smallView = webview()
val stage = Stage(StageStyle.UTILITY)
stage.setScene(Scene(smallView))
stage.show()
val engine = smallView.getEngine()
return engine
}
})
I have been searching for a long time on the internet, but I didn't find anything. Please can somebody help me fix this error.
You're almost there :) To create an anonymous class much the same way you do in Java, you need to use the object keyword in front of the class statement:
br.createPopupHandler = object : Callback<PopupFeatures, WebEngine> {
However, Kotlin allows you to turn SAM types into lambdas. You can also utilize the property access pattern and immutable values to clean up the code a little. Here is a the code rewritten and Kotlinified:
val wv = webview()
val br = wv.engine
br.setCreatePopupHandler {
val smallView = webview()
val stage = Stage(StageStyle.UTILITY)
stage.scene = Scene(smallView)
stage.show()
smallView.engine
}
I haven't really evaluated what you're doing here or if that's a good idea, so only take my advice on the syntax :)

Share data in paged based interface in Watchkit

I need to share a string with all the pages from the first page.
I was trying to do this with didDeactive() but its not working.
Is it possible to even do this?
It's a bit difficult to tell what you are really trying to do, so I'm going to infer a bit. I "think" you are trying to set some shared value from the first page and be able to use that value in other pages. If that's the case, then you could do something like the following:
class SharedData {
var value1 = "some initial value"
var value2 = "some other initial value"
class var sharedInstance: SharedData {
struct Singleton { static let instance = SharedData() }
return Singleton.instance
}
private init() {
// No-op
}
}
class Page1InterfaceController: WKInterfaceController {
func buttonTapped() {
SharedData.sharedInstance.value1 = "Something new that the others care about"
}
}
class Page2InterfaceController: WKInterfaceController {
#IBOutlet var label: WKInterfaceLabel!
override func willActivate() {
super.willActivate()
self.label.setText(SharedData.sharedInstance.value1)
}
}
The SharedData object is a singleton class that is a global object. Anyone can access it from anywhere. In the small example I provided, the Page1InterfaceController is handling a buttonTapped event and changing the value property of the SharedData instance to a new value. Then, when swiping to Page2InterfaceController, the new value is being displayed in label.
This is a super simple example of sharing data between objects. Hopefully that helps get you moving in the right direction.

swift empty dictionary bug? playground vs project

this code work in playground
var detaildata:Dictionary=[:]
detaildata = ["apple":"hello"]
detaildata["orange"]="byebye"
this code don't work in project
class ViewController: UIViewController{
var detaildata:Dictionary=[:]
override func viewDidLoad() {
super.viewDidLoad()
detaildata = ["apple":"hello"]
detaildata["orange"]="byebye" // Error -> 'Dictionary' is not identical to 'Dictionary<key,Value>'
}
}
do you know why?
I think same code.
That code doesn't work in my playground, and shouldn't work in yours. When you declare a Dictionary, you need to give it both key and value types either explicitly or through type inference. All of these will work:
var dict1 = ["apple": "hello"] // inferred
var dict2: [String: String] = [:] // explicit
var dict3: Dictionary<String, String> = ["apple": "hello"] // longest version
Neither of these will work:
var dict3: Dictionary = [:] // type inference impossible
var dict4 = [:] // same

Best way to typecast deserialised JSON

I think I've established that in as3corelib JSON.decode I have no choice but to deserialise to a plain old flex object.
var data:Object = JSON.decode(json);
If I then want to get the data contained in the object into another type I can't use type casting. I have to instantiate a new instance and add the properties manually.
var data:Object = JSON.decode(json);
var model:Model = new Model();
model.name = data.name;
model.notes = data.notes;
A pain and a bit ugly, but I'm guessing this is the price to be paid for going from untyped json to a flex type. My first question is whether my assumption is correct and there is no prettier way to create my model instance with the data contained within the json?
My second question, if so then before I write my own method to do this, is there anything inside the flex api that will take the data object and mixin it's values to my model instance?
Cheers,
Chris
the approach I've always used proved to be part of the AMF3 serialization mechanism in ActionScript.
have a look at IExternalizable and registerClassAlias.
now what I use is the following:
interface ISerializable {
public function getRawData():Object;
public function setRawData(param:Object):void;
}
function registerType(id:String, type:Class):void {
//implementation
}
function getTypeByID(id:String):Class {
//implementation
}
function getTypeID(type:Class):String {
//implementation
}
and to the decoder/encoder you register a class alias.
serialization of an object works as follows:
var raw:Object = model.getRawData();
raw[" type"] = getTypeID(model);
var encoded:String = JSON.encode(raw);
decoding works as follows:
var raw:Object = JSON.decode(raw);
var cl:Class = getTypeByID(raw[" type"]);
if (cl == null) throw new Error("no class registered for type: "+raw[" type"]);
delete raw[" type"];
var model:ISerializable = new cl();
model.setRawData(raw);
you will need to do this recursively on the whole deserialized JSON tree, starting at the leafs.
For cyclic reference, you'll need a trick.
I had an implementation of this somewhere, but I can't find it.
You can loop within the field of you json decoded object and assign them into your model:
function json2model(json:String):Model{
var data:Object = JSON.decode(json);
var m:Model=new Model();
for (var field:String in data) {
if (m.hasOwnProperty(field)) {
m[field] = data[field];
}
}
return m;
}
var model:Model=json2model(json)
or add a static function within your Model if you preffer:
public class Model {
//...
public static function fromJSon(json:String):Model {
var data:Object = JSON.decode(json);
var m:Model=new Model();
for (var field:String in data) {
if (m.hasOwnProperty(field)) {
m[field] = data[field];
}
}
return m;
}
}
}
var model:Model=Model.fromJSon(json);

Is it possible to define a generic type Vector in Actionsctipt 3?

Hi i need to make a VectorIterator, so i need to accept a Vector with any type. I am currently trying to define the type as * like so:
var collection:Vector.<*> = new Vector<*>()
But the compiler is complaining that the type "is not a compile time constant". i know a bug exists with the Vector class where the error reporting, reports the wrong type as missing, for example:
var collection:Vector.<Sprite> = new Vector.<Sprite>()
if Sprite was not imported, the compiler would complain that it cannot find the Vector class. I wonder if this is related?
So it looks like the answer is there is no way to implicitly cast a Vector of a type to valid super type. It must be performed explicitly with the global Vector.<> function.
So my actual problem was a mix of problems :)
It is correct to use Vector. as a generic reference to another Vector, but, it cannot be performed like this:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = spriteList // this will cause a type casting error
The assignment should be performed using the global Vector() function/cast like so:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = Vector.<Object>(spriteList)
It was a simple case of me not reading the documentation.
Below is some test code, I would expect the Vector. to cast implicitly to Vector.<*>.
public class VectorTest extends Sprite
{
public function VectorTest()
{
// works, due to <*> being strictly the same type as the collection in VectorContainer
var collection:Vector.<*> = new Vector.<String>()
// compiler complains about implicit conversion of <String> to <*>
var collection:Vector.<String> = new Vector.<String>()
collection.push("One")
collection.push("Two")
collection.push("Three")
for each (var eachNumber:String in collection)
{
trace("eachNumber: " + eachNumber)
}
var vectorContainer:VectorContainer = new VectorContainer(collection)
while(vectorContainer.hasNext())
{
trace(vectorContainer.next)
}
}
}
public class VectorContainer
{
private var _collection:Vector.<*>
private var _index:int = 0
public function VectorContainer(collection:Vector.<*>)
{
_collection = collection
}
public function hasNext():Boolean
{
return _index < _collection.length
}
public function get next():*
{
return _collection[_index++]
}
}
[Bindable]
public var selectedItems:Vector.<Category>;
public function selectionChange(items:Vector.<Object>):void
{
selectedItems = Vector.<Category>(items);
}
I believe you can refer to an untyped Vector by just calling it Vector (no .<>)
With Apache Flex 4.11.0, you can already do what you want. It might have been there since 4.9.0, but I have not tried that before.
var collection:Vector.<Object> = new Vector.<Object>()
maybe?
But i'm just speculating, haven't tried it.
var collection:Vector.<Object> = new Vector.<Object>()
but only on targeting flash player 10 cs4

Resources