im new in Swift and X-Code. Im searching for a way import a own defined class (best case in an own document). I want store all needed settings (text-snippets and app-settings) in this file. For example:
class snippetsAndSettings {
// e.g translations
let t_welcome: String = "Welcome to my App"
let t_share: String = "Social Share Buttons"
let t_rate: String = "Rate"
// e.g settings
let s_weburl: String = "http://www.mypage.com/webservice.php"
let s_slider: Bool = false
let s_bgColor: String = "#ff9900"
let s_tColor: String = "#222222"
let t_shadow: String? // Bool or colorString!
}
I want to use this class on every app-page. But i can not import the for for example in ViewController.swift
My questions:
In which format do i have to save the file (snippetsAndSettings)? Cocoa Class
How can i import the file in my ViewController.Swift
Is this a common way to store own app-settings in Swift?
The method to store settings is via NSUserDefaults. You can setup your default values in your app delegate's application:didFinishLaunchingWithOptions: method. For example:
NSUserDefaults.standardUserDefaults().registerDefaults([
"volume": 100,
"showShadows": true
])
To get these back, you would do:
NSUserDefaults.standardUserDefaults().integerForKey("volume")
and
NSUserDefaults.standardUserDefaults().boolForKey("showShadows")
In Swift 5, it now looks like this:
UserDefaults.standard.register(defaults: [
"volume": 100,
"showShadows": true
])
And to get it back
let volume = UserDefaults.standard.integer(forKey: "volume")
let showShadows = UserDefaults.standard.bool(forKey: "showShadows")
For translations, you should look in to Internationalization and Localization
Related
I am using your new BlinkIDUI sdk for iOS and I can have the list of all the scanned fields from "recognitionResult.resultEntries" like Secondary ID = Jason", "Primary ID = Bourne", "Sex = F", "Date Of Birth = 3/23/83", "Nationality = UAE", "Document Code = P" from the delegate method "- (void)didScanEntireDocumentWithRecognitionResult:(MBRecognitionResult * _Nonnull)recognitionResult successFrame:(UIImage * _Nullable)successFrame". My query is How to get value for particular key like “"Document Code” ?
Additional Details are:
The Framework addition in Project: Manual.
Xcode version : 10.1.
Language: Objective-C (ARC OFF).
Device: iPhone8 / iOS(11.1.1)
That's because resultEntries is an array not a dictionary,
Use like this:
for (MBField *field in recognitionResult.resultEntries) {
if (field.key == MBFieldKeyDocumentCode) {
}
}
If you are using it in ObjectiveC project then also check if #objc tag is there in front of MBFieldKey public property in "MBField" class, if it is not there just put it as:
public class MBField: NSObject {
#objc public let key: MBFieldKey
#objc public let value: String
.....
}
i work with swift 3 and nsoutlineview.
i would like to save the objectID of a core data record into a textfield.
so i have to convert it into a string:
txtFied.stringValue = "\(CoreData[outlineView.selectedRow].objectID)"
how can i convert it back to an NSManagedObjectID?
I've done this via the persistent store coordinator's managedObjectID(forURIRepresentation:) method, as outlined below:
// Convert NSManagedObjectID to a string, via the uriRepresentation method.
let objectIDString = <your managed object ID>.uriRepresentation().absoluteString
...
// Use the persistent store coordinator to transform the string back to an NSManagedObjectID.
if let objectIDURL = URL(string: objectIDString) {
let coordinator: NSPersistentStoreCoordinator = <reference to your persistent store coordinator>
let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectIDURL)
}
I am attempting to integrate this library with my RxSwift project, https://github.com/glenyi/FloatRatingView
I am unable to get the updated rating.
Here is how I have created the FloatRatingView in the Controller,
let starRater : FloatRatingView = {
let floatRatingView = FloatRatingView()
floatRatingView.emptyImage = UIImage(named: "EmptyStar")
return floatRatingView
}()
The Model contains the following,
let my_rating = Variable<Float?>(nil)
What I want to be able to do is to update the value in the my_rating variable when a user changes the rating by tapping on a star. Here is what I've written for this,
_ = starRater.rx.rating
.asObservable()
.bindTo(viewModel.my_rating.rx_value)
But here is the error I'm receiving.
Value of type 'Reactive FloatRatingView' has no member 'rating'
Here is how I will retrieve the value from my_rating variable,
let stars = self.my_rating.value
Kindly help me out. Thanks.
You need to add a bindable sink for the property, it would be something like this:
extension Reactive where Base: FloatRatingView {
/// Bindable sink for `rating` property
public var progress: UIBindingObserver<Base, Float> {
return UIBindingObserver(UIElement: self.base) { floatRatingView, rating in
floatRatingView.rating = rating
}
}
}
I have a variable that I made. I made it so that all Scenes (there's three) know what it is. I called it "money" and attached an Interger to it. But it won't save. So when I go my second scene to add $20 to the money which now the money should have $20 in it. When I go back to the first scene to check it its not there it says there are zero dollars. Does anyone know how to make a money system that all Scenes can recognize and add money to that "Money" variable. Kind of like how you would save a high score and know matter what even if you close the app the high score is still saved
A solution to manage global variables is a Singleton class.
The class gets instantiated the first time it's called and persists during the life time of the application.
The class contains the mentioned variable money as well as a dummy string variable somethingElse. You can add as many variables as you need.
class DefaultsManager {
// MARK: - Variables
var money : Int = 0
var somethingElse : String = ""
// MARK: - Shared Instance
class var sharedManager : DefaultsManager {
struct Singleton {
static let instance = DefaultsManager()
}
return Singleton.instance
}
init()
{
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["money" : 0, "somethingElse" : ""];
defaults.registerDefaults(defaultValues)
}
func readDefaults()
{
let defaults = NSUserDefaults.standardUserDefaults()
money = defaults.integerForKey("money")
somethingElse = defaults.stringForKey("somethingElse")!
}
func saveDefaults()
{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(money, forKey:"money")
defaults.setObject(somethingElse, forKey:"somethingElse")
defaults.synchronize()
}
}
The class is accessible from everywhere.
Get the shared instance
let manager = DefaultsManager.sharedManager
Get the value of the variable money
var currentDeposit = manager.money
Read and write the variables from user defaults
manager.readDefaults()
manager.saveDefaults()
If you add more variables, you have to register them in init and to add lines to read and save them according to the other variables.
I am using Swift, Cocoa Bindings and Core Data in an OSX Xcode project to display an outline view which is bound to my entity "Series" and displays the attribute "name", which has a to-many relationship with itself.
I want my users to be able to enter only three levels: one root, a child and another child of that child.
Here is my subclass for Series:
#objc(Series)
class Series: NSManagedObject {
#NSManaged var isLeaf: NSNumber
#NSManaged var name: String
#NSManaged var parent: Series
#NSManaged var subGroups: NSSet
}
Effectively when the data is entered and saved, "isLeaf" should be changed to true for the third name entry. In this way, it would be impossible for the user to create a fourth entry.
I added this function to the Series subclass, so I could validate each new name entry:
func validateName(ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>,
error: NSErrorPointer) -> Bool {
if let test = ioValue.memory as? String {
if test != "" {
println("Name is \(test)")
// This is where we need to test for indentation level
}
} else {
}
return true
}
I'm pretty sure that what I need to do is test for the indentation level of the third entry and if it returns 2, then I need to change isLeaf from false to true.
Unfortunately, I do not know how to do this in practice. Does anyone have any suggestions, please?