Swift - Define properties inside class - ios

I was learning Swift and developing my test project on first XCode Beta, but few days ago I've downloaded XCode Beta4 and that's showed error in build.
My code:
class LoginModalViewController: UIViewController {
#IBOutlet var usernameField : UITextField
#IBOutlet var passwordField : UITextField
#IBOutlet var loginButton : UIButton
...
Previously that's how I defined outlets inside controller, but in new beta it gives me an error IBOutlet property has non-optional type UITextField and suggests me to fix it in 2 ways:
add ! after each var
add ? after each var
I know that ? means optional and if I'll choose it then I need to update my code like usernameField.becomeFirstResponder() into usernameField!.becomeFirstResponder()
Adding ! solves errors perfectly..
So as I understand there are only these two ways to create properties inside class, am I right?
It will be handy to find the changelog of such Swift syntax language updates.

Actually, something like var usernameField : UITextField is a perfectly valid property declaration in Swift, even though it isn't declared as an optional. In your version of Swift, the issue you were having is that properties annotated with #IBOutlet must be optionals.
Changes to the Swift language may be found in the Xcode 6 beta release notes, here.
On a side note, since you mentioned you'd updated to Xcode Beta 4, I'd like to point out that at the time of writing, Xcode Beta 5 was just released 2 days ago :)

Related

Realm crash on iOS 10 with 'String'

I have recently released a new version of our app and during beta testing, it's crashing on all iOS 10 devices but not other versions. Since we have Crashlytics, we found a strange crash message in the backend that we can confirm is the reason all iOS 10 crashing since it's 100% iOS 10 and there's like 40 of them.
It reads as follows:
Fatal Exception: RLMException
Property Article.id is declared as String, which is not a supported managed Object property type. If it is not supposed to be a managed property, either add it to ignoredProperties() or do not declare it as #objc dynamic. See https://realm.io/docs/swift/latest/api/Classes/Object.html for more information.
And here's the object:
class Article: Object {
#objc dynamic var id: String = UUID().uuidString
// others...
override static func primaryKey() -> String? {
return "id"
}
}
As you can see, this is perfectly nomral and runs fine on other iOS. In Realm's doc, it LITERALLY SAYS to use String with #objc dynamic and there's no way it's unsupported. I suspect there's nothing special about Article.id, and since Article starts with A, it happens to be the first String property of all realm Objects. Maybe somehow all Strings stopped working on iOS 10?
Can anyone offer some advice or insights?(Please don't say things like drop iOS 10 support. For now, we need it.)
We ran into the same issue a couple of times, trying to drag Realm fully into Swift. This is not really the answer but more of a workaround we've had success with when needing backward compatibility.
It's an ObjC object, not Swift.
There's something going on with the bridging, perhaps conforming to NSCopy'ing or something along those line, so just change it to read
#objc dynamic var id = NSUUID().uuidString
See the Getting Started Guide in the Models section which calls for using NSUUID
NSUUID: An object representing a universally unique value that bridges
to UUID; use NSUUID when you need reference semantics or other
Foundation-specific behavior.
Turns out it was a Realm's bug. We happen to have another app that runs just fine on iOS 10, and after some inspection we realized that it was using Realm 4.3.2, instead of 4.4.1. After we downgraded Realm to 4.3.2, this problem disappeared.

Can anyone explain how to obfuscate codebase using Swiftshield?

I have gone through this post but I am still not able to obfuscate full codebase.
I am using this command for the same:
swiftshield -automatic -project-root ../TestApp -automatic-project-file ../TestApp/DemoTestApp.xcworkspace -automatic-project-scheme DemoTestApp
Getting this error:
error: 'CustomButton' is unavailable: cannot find Swift declaration for this class
#IBOutlet weak var customButton: CustomButton!
^~~~~~~~~~
DemoFramework.CustomButton:2:12: note: 'CustomButton' has been explicitly marked unavailable here
I have a CustomButton class that is in the custom framework.
Can anyone walk me step by step methods for how to use swiftshield for code obfuscation?
Thanks in advance. :)

Swift App Extension: instance method count is unavailable

I just create my first app extension using XCode 7.1. One code file containing the code below is shared with both targets:
var str = "";
var l = str.count; //Compile error for extension target App: count is unavailable: There is no ...
The reason for this compile error seams to be that App extension compiles with swift 1.2 while the container target compiles with swift 2.0.
One solution would be importing the content App into the extension App doesn't appear to be a good solution from what i read about it. Sharing the code between targets can be difficult if both are not compiled using the same compiler.
I just run through all target settings and didn't find nothing that could be changed.
Can't find any post about this problem, witch is not so uncommon, so it is must likely i am interpreting something in a wrong way.
The only solution i can think of is using NSString instead of String but that is just an workaround for one class type. More problems of this kind will emerge in the future.
In Swift 2 it's
str.characters.count
Use str.characters.count to get String length in Swift 2

Parse SDK and Swift: Incorrect argument label in call PFObject 'withoutDataWithObjectId'

I subclass PFObject exactly as described here.
Then I create a new instance of the subclassed object without data, but since Swift 1.2 I get an error (It did work perfectly before):
var test = Armor(withoutDataWithObjectId: "1234567890")
-> Xcode complains:
"Incorrect argument label in call (have 'withoutDataWithObjectId:',
expected: 'className:')"
Why className? It should get the class name from the class function parseClassName
And I can under no circumstances create a new object with objectId but no data (which I MUST have to fetch it from the local datastore)
This is super annoying as my app doesn't compile any longer.
Update to the newest Parse SDK, available here.
The issue is caused due to necessary adaptions in the Parse SDK after the Swift language update. This issue also occurs with the most recent update to Swift 2.2. The newest (as of today) Parse SDK release 1.13.0 already fixes this.
UPDATE
Parse iOS SDK 1.13.0 has a typo and the function PFUser(withoutDataWithObjectId:) is called PFUser(outDataWithObjectId:). So upgrading the Parse SDK alone does solve this. Until this is fixed a temporary workaround would be to extend PFObject with a convenience initializer. To do this add a new Swift file to your project and insert this:
import Parse
extension PFObject {
convenience init(withoutDataWithObjectId objectId: String?) {
self.init(outDataWithObjectId: objectId)
}
}
It may be a little late to answer this question.
I use swift 1.2, and v 1.7.5 Parse SDK, and it works totally fine.
however, make sure you have define objective-c bridging header in "build setting".
and try to run it, even though there may reports some error

uppercaseString of IBOutlet UITextField.text crash

I'm running Xcode 6.3 beta 1 with iOS 8.3 beta 1 in the iOS Simulator. I have a UITextField #IBOutlet of which I'm trying to get the uppercaseString property of its text property, like this:
#IBOutlet weak var field: UITextField!
#IBAction func calledAfterUserAction(){
let capitalized = field.text.uppercaseString
}
The above workflow will cause a crash. After turning on Zombie Objects, I can see the following error:
*** -[CFString release]: message sent to deallocated instance 0x7b689cd0
How can I fix this?
I just ran into the same problem - very glad you posted this so that I knew I wasn't crazy!
I've discovered that the simple "uppercaseString" method fails with a deallocated object error, while the extended version that takes a Locale works. I'm guessing there is an internal Apple bug over handling default locales... Change your code to:
let capitalized = field.text.uppercaseStringWithLocale(NSLocale.currentLocale())

Resources