Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
// TODO: - This is often used combination in regular projects to get access to project resources
let mainBundle = Bundle.main
print("identifier: \(String(describing: mainBundle.bundleIdentifier))")
// TODO: - Find nessesary bundle, for example by name
let allBundles = Bundle.allBundles
print("bundles: \(allBundles)")
let mainBundle = Bundle.main
print("identifier: \(String(describing: mainBundle.bundleIdentifier))")
let allBundles = Bundle.allBundles
print("bundles: \(allBundles)")
class User {
var id: UInt
var name: String
init(id: UInt, name: String) {
self.id = id
self.name = name
}
let identifierExample = "com.apple.dt.playground.stub.iOS_Simulator.Bundle-FF59A7A5-B1B5-4AEB-AE77-2334AF184375"
let identifiedBundle = Bundle(identifier: identifierExample)
print("identifier bundle: \(String(describing: identifiedBundle))")
/*
Note: Result of loadNibNamed is array of common level views from Nib
Usually the view you need is the first
*/
let customView = classBundle.loadNibNamed("CustomView", owner: nil, options: nil)?.first
guard let jsonURL = classBundle.url(forResource: "users", withExtension: "json"),
let data = try? Data(contentsOf: jsonURL),
let objects = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) else { fatalError("haven't users.json file") }
print("objects: \(objects)")
/*
objects: {
users = (
{
id = 190;
let localizations = classBundle.localizations
print("localizations: \(localizations)")
import Foundation
let fileSizeInBytes: Int64 = 1924284
let byteFormatter = ByteCountFormatter()
print("Default settigs: \(byteFormatter.string(fromByteCount: fileSizeInBytes))")
// Default settigs: 1.9 MB
byteFormatter.allowedUnits = .useKB
print("Change allowedUnits settig: \(byteFormatter.string(fromByteCount: fileSizeInBytes))")
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.unitsStyle = .full
dateComponentsFormatter.includesApproximationPhrase = true
dateComponentsFormatter.includesTimeRemainingPhrase = true
dateComponentsFormatter.allowedUnits = [.minute, .hour]
let timeIntervalInPast: TimeInterval = -3666
let nowDate = Date()
let pastDate = nowDate.addingTimeInterval(timeIntervalInPast)