I want a dictionary out of my plist, which i can use in the whole gamescene. However in my solution i always have to call the parseConfig function in order to get a dictionary from the plist.
struct Config: Decodable {
private enum CodingKeys: String, CodingKey {
case zPositions, enemy, player
}
let zPositions: [String:Double]
let enemy: [String:[String:Double]]
let player: [String:[String:Double]]
}
func parseConfig() -> Config {
let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}
try crate lazy property.
Something like: - if use gloabaly
var parsedConfig: Config = {
let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}()
if in class add lazy before var
You can try to write a closure to load only once
lazy var myConfig : Config = {
let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}()
or inside a singleton in all the app
class Service {
static let shared = Service()
lazy var myConfig : Config = {
let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}()
}
Related
For some reason, Microsoft AdaptiveCards dropdown is not working on iOS.
What I'm I missing?
Here's the Json:
Json
Here's the code I'm using to render the card:
let url = Bundle.main.url(forResource: "adaptiveCard", withExtension: "json")!
let data = try! Data(contentsOf: url)
let jsonStr = String(data: data, encoding: String.Encoding.utf8)
let cardParseResult = ACOAdaptiveCard.fromJson(jsonStr);
let config = ACOHostConfig()
if((cardParseResult?.isValid)!) {
let renderResult = ACRRenderer.render(cardParseResult!.card, config: config, widthConstraint: Float(bounds.width))
if(renderResult?.succeeded ?? false) {
if let view = renderResult?.view {
context.coordinator.setDelegate(view: view)
return view
}
}
}
Here's a screenshot of the screen:
I have an array of custom Plant Objects. Plant is Codable. I use JSONEncoder().encode() to get the array encoded in JSON, but how do I store this JSON, so that it can be saved once the app closes? I remember with NSCoder I could just encode it when the app closes and use the required convenience init? to decode it, but i don't see a similar option here. Here is my Singleton Class in which I am trying to save the [Plant]
import Foundation
public class Singleton{
static let sInstance = Singleton(mPL: [Plant]())
var myPlantList: [Plant]
init(mPL: [Plant]){
self.myPlantList = mPL
}
public func savePlants(){
let jsonData = try? JSONEncoder().encode(myPlantList)
}
}
Helper extensions..
import Foundation
public extension FileManager {
// Returns a URL that points to the document folder of this project.
static var documentDirectoryURL: URL {
return try! FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
)
}
}
Creating a folder that will contain your data file
let documentSubdirectoryURL = URL(
fileURLWithPath: "MyFolder",
relativeTo: FileManager.documentDirectoryURL
)
try? FileManager.default.createDirectory(
at: documentSubdirectoryURL,
withIntermediateDirectories: false
)
Saving -
do {
let yourURL = URL(fileURLWithPath: "yourFileName", relativeTo: FileManager.documentDirectoryURL.appendingPathComponent("MyFolder")).appendingPathExtension("swift")
///ENCODE...
try jsonData.write(to: yourURL)
}
Decode -
do {
let yourURL = URL(fileURLWithPath: "yourFileName", relativeTo: FileManager.documentDirectoryURL.appendingPathComponent("MyFolder")).appendingPathExtension("swift")
let jsonDecoder = JSONDecoder()
let data = try Data.init(contentsOf: yourURL)
let value = try jsonDecoder.decode([Plant].self, from: data)
}
If you want to check the file containing your data, navigate to the url returned by
FileManager.documentDirectoryURL
Try
jsonData.write(to: url)
I am trying to create my own content blocker on iOS. I was wanting to create separate json lists for different types of content (tracking, ads, adult sites, etc). I came across this https://github.com/calebhicks/ios-safari-content-blocking which stated you could create an array of "attachments" instead of relying on the singular "blockerList" json file.
func beginRequest(with context: NSExtensionContext) {
var jsonFiles:Array<NSItemProvider> = Array()
let attachment = NSItemProvider(contentsOf: Bundle.main.url(forResource: "blockerList", withExtension: "json"))!
jsonFiles.append(attachment)
let attachment2 = NSItemProvider(contentsOf: Bundle.main.url(forResource: "testList", withExtension: "json"))!
jsonFiles.append(attachment2)
let item = NSExtensionItem()
item.attachments = jsonFiles
context.completeRequest(returningItems: [item], completionHandler: nil)
}
Most of this code is the default from the Content Blocker Extension setup, but what I have added is the jsonFiles array which attachment and attachment2 are placed in. When this is run, only one of the two rule sets is loaded, never a combination of the two. Any ideas on why only one ruleset is loaded?
You can combine two JSON rule files in to one file and use that file.
import UIKit
import MobileCoreServices
class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {
func beginRequest(with context: NSExtensionContext) {
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "you app group identifier")
let sourceURLRules = sharedContainerURL?.appendingPathComponent("Rules1.json")
let sourceURLRules2 = sharedContainerURL?.appendingPathComponent("Rules2.json")
do {
let jsonDecoder = JSONDecoder()
let dataFormRules1 = try Data(contentsOf: sourceURLRules1!, options: .mappedIfSafe)// Rule is Decode able Swift class
let rulesArray1 = try? jsonDecoder.decode(Array<Rule>.self,from: dataFormRules1)
let dataFormRules2 = try Data(contentsOf: sourceURLRules2!, options: .mappedIfSafe)
let rulesArray2 = try? jsonDecoder.decode(Array<Rule>.self,from: dataFormRules2)
saveCombinedRuleFile(ruleList: rulesArray1! + rulesArray2!)
} catch {
//handle error condition
}
let sourceURLCombinedRule = sharedContainerURL?.appendingPathComponent("CombinedRule.json")
let combinedRuleAttachment = NSItemProvider(contentsOf: sourceURLCombinedRule)
let item = NSExtensionItem()
item.attachments = [combinedRuleAttachment]
context.completeRequest(returningItems: [item], completionHandler: nil)
}
func saveCombinedRuleFile(ruleList:[Rule]) {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(ruleList) {
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "you app group identifier")
if let json = String(data: encoded, encoding: .utf8) {
print(json)
}
if let destinationURL = sharedContainerURL?.appendingPathComponent("CombinedRule.json") {
do {
try encoded.write(to: destinationURL)
} catch {
print ("catchtry")
}
}
}
}
}
ok guys can I get a little help with my code. When running the app I get an error is there any way to fix this problem?
let fileUrl = dict["fileUrl"]as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOf: url! as URL!)
let picture = UIImage(data: data! as Data!)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
Image here
Here I see 4 big fat problems with your code.
You are force casting the value of fileUrl of the dictionary dict to String. If your dictionary doesn't have the value for fileUrl, or if it's not castable to string, your code will crash. You should change that to optional cast like:
if let fileUrl = dict["fileUrl"] as? String
{
//your code if you have fileUrl
}
When creating the url to the file, you are using the wrong initialization method, you should be using this:
let url = URL(fileURLWithPath: fileUrl)
After you have the url to the file, you should also check if you have the data of the file, because contentsOfFile: initializer of the NSData returns the optional object, which may be nil, so another if check:
if let data = NSData(contentsOf: url) {\\ code with the data}
init?(data: Data) initializer of the UIImage also returns optional object, so if the required by latter code, you should also check if you have the image or nil with if statement.
The result code should be something like:
if let fileUrl = dict["fileUrl"] as? String {
let url = URL(fileURLWithPath: fileUrl)
if let data = NSData(contentsOf: url) {
let image = UIImage(data: data as Data) // you can cast NSData to Data without force or optional casting
let photo = JSQPhotoMediaItem(image: image)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
}
}
Hope this helps.
Replace the first line of code with this line for optional binding check :-
guard let fileUrl = dict["fileUrl"] as! String else {return}
Yo should do validation in cases where the variable may be nil, the following is an example:
if let fileUrl = dict["fileUrl"] as? String {
let url = URL(string: fileUrl)
do {
let data = try Data(contentsOf: url!)
let picture = UIImage(data: data)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
} catch {
}
}
Xcode 7 Playgrounds now supports loading files from the nested Resources directory.
I can get SKScene(fileNamed: "GameScene") when I have a GameScene.sks in my Resources or NSImage(named:"GameScene.png") if I have a GameScene.png in your Resources.
But how can I read a regular text file from the Playground Resources directory as well?
We can use the Bundle.main
So, if you have a test.json in your playground like
You can access it and print its content like that:
// get the file path for the file "test.json" in the playground bundle
let filePath = Bundle.main.path(forResource:"test", ofType: "json")
// get the contentData
let contentData = FileManager.default.contents(atPath: filePath!)
// get the string
let content = String(data:contentData!, encoding:String.Encoding.utf8)
// print
print("filepath: \(filePath!)")
if let c = content {
print("content: \n\(c)")
}
Will print
filepath: /var/folders/dm/zg6yp6yj7f58khhtmt8ttfq00000gn/T/com.apple.dt.Xcode.pg/applications/Json-7800-6.app/Contents/Resources/test.json
content:
{
"name":"jc",
"company": {
"name": "Netscape",
"city": "Mountain View"
}
}
Jeremy Chone's answer, updated for Swift 3, Xcode 8:
// get the file path for the file "test.json" in the playground bundle
let filePath = Bundle.main.path(forResource: "test", ofType: "json")
// get the contentData
let contentData = FileManager.default.contents(atPath: filePath!)
// get the string
let content = String(data: contentData!, encoding: .utf8)
// print
print("filepath: \(filePath!)")
if let c = content {
print("content: \n\(c)")
}
You can use String directly with a URL. Example in Swift 3:
let url = Bundle.main.url(forResource: "test", withExtension: "json")!
let text = String(contentsOf: url)
Swift 5
It is possible to get to files in your Resources folder by the bundle in a Playground.
import UIKit
Here are two ways to get the JSON data.
Path:
guard let path = Bundle.main.path(forResource:"test", ofType: "json"),
let data = FileManager.default.contents(atPath: path) else {
fatalError("Can not get json data")
}
URL:
guard let url = Bundle.main.url(forResource:"test", withExtension: "json") else {
fatalError("Can not get json file")
}
if let data = try? Data(contentsOf: url) {
// do something with data
}
Another short way (Swift 3):
let filePath = Bundle.main.path(forResource: "test", ofType: "json")
let content: String = String(contentsOfFile: filePath!, encoding: .utf8)
Added try for swift3.1:
let url = Bundle.main.url(forResource: "test", withExtension: "json")!
// let text = String(contentsOf: url)
do {
let text = try String(contentsOf: url)
print("text: \n\(text)")
}
catch _ {
// Error handling
}
// --------------------------------------------------------------------
let filePath2 = Bundle.main.path(forResource: "test", ofType: "json")
do {
let content2: String = try String(contentsOfFile: filePath2!, encoding: .utf8)
print("content2: \n\(content2)")
}
catch _ {
// Error handling
}