How to access a Cocoa Touch Framework module on Playground Swift

Asked

Viewed 141 times

2

I created a Cocoa Touch Framework module on Xcode 6.0.1 with Class implemented in Swift and can matter in my View Controller and use it normally. The problem that I can’t use the same code in the Playground created in the same project.

In the Playground the module is recognized but the class contained in it is not.

inserir a descrição da imagem aqui

My class is declared in JSON.Swift so:

public class JSON {
    // meu código Swift 
}

2 answers

1

Try this on your Playground from Xcode 6.0.1

import Foundation
let jsonObject: [AnyObject] = [
  ["name": "João", "age": 20],
  ["name": "Pedro", "age": 45],
]
func JSONStringify(jsonObj: AnyObject) -> String {
  var e: NSError?
  let jsonData: NSData! = NSJSONSerialization.dataWithJSONObject(
    jsonObj,
    options: NSJSONWritingOptions(0),
    error: &e)
  if e != nil {
    return ""
  } else {
    return NSString(data: jsonData, encoding: NSUTF8StringEncoding)
  }
}
let jsonString = JSONStringify(jsonObject)
println(jsonString)

Works in the Mavericks

This way it works because you are delegating the work of serialization to class NSJSONSerialization of the standard library

Meaning, your Swift code is just a Wrapper.

Deserialization can also be done in an analogous way.

-1

By default classes have visibility internal. It’s only necessary to make the class public and you get the access.

public class Foo {
    public init() {
    }
}
  • the class is already as published. See the description of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.