uppercaseString of IBOutlet UITextField.text crash - ios

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())

Related

Swinject: Using weak scope is leaking objects

I'm using Swinject for DI in my swift project. I think using weak object scope is leading to leaks.
I have this dummy object
protocol DevTestProtocol: class {}
class DevTest: DevTestProtocol {}
which is registered like follows (I'm using an assembly)
container.register(DevTestProtocol.self) { _ in
return DevTest()
}
.inObjectScope(.weak)
For testing purposes I added a property in my app delegate
var devTest: DevTestProtocol?
and in applicationDidFinishLaunchingWithOptions I resolve the object then forget about it
self.devTest = DI.resolve(DevTestProtocol.self)!
self.devTest = nil
Is the DevTest instance supposed to be gone by now?
Because it's not. It still shows in the debug memory graph as a leak.
see screenshot
I guess this isn't expected behaviour? Am I missing something?
It looks like a bug to me and it's messing up my whole setup.
What can we do about this?
GitHub issue
There is no memory leak, it's just a bug in Xcode 8 memory profiling instruments. See discussing at GitHub issues tracker

Xcode 8 Core Data abnormality

I am trying to update my project to Swift 3 which has Core Data. I have confronted with serious error and I really don't know what is going on. My Core Data Model has following properties
#NSManaged var name: String?
#NSManaged var count: NSNumber
#NSManaged var isDelivered: NSNumber
I can set any other properties but isDelivered. When I try to use
myobject.isDelivered = true
I get following error on the console.
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[MyModel setDelivered:]:
unrecognized selector sent to instance 0x6000000d3780'
It looks like Xcode is removing is part from isDelivered property which crashes the app. Is there anything that I can do to prevent this other than updating my model? Thanks.
It is a BUG. It is very confusing bug. For anyone visiting this question, here is the answer I found on Apple forums.
The current version of Swift 3 beta seems to have some flaw about treating properties prefixed with "is".
https://forums.developer.apple.com/thread/50863
Answer from user OOPer
Avoid using "is" in your property name, or if you cannot, try this:
newWriter.setValue(true, forKey: "favorite")
(Update)
Try adding #objc name to the property:
#NSManaged #objc(isFavorite) var isFavorite: Bool

PureLayout on device: unrecognized selector sent to instance

today I found out that there is an issue with my project. I can't run it on a real device, because I am getting an 'unrecognized selector sent to instance' error when it reaches the part of code that comes from a pod that I am using. I can run the app if I comment the part with this code.
The problem here is that I did not encounter this problem before and it just came out of nothing.
Under linked frameworks and libraries, my pod is linked as 'optional', because I get 'dyld: Library not loaded' error if it is marked as 'required'. Bitcode is enabled as well, changing this didn't make any difference.
Currently I am using XCode 7.0 (7A220) and Cocoapods version 0.38.2.
I'd be glad for any given tips on this.
#Edit
I've found out, that I was able to run other pods. The issue comes with PureLayout pod.
#Edit2 Code
class MainViewController: UIViewController {
var label : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
func setupView() {
self.view.backgroundColor = UIColor.whiteColor()
label = UILabel()
self.view.addSubview(label)
label.autoPinEdgesToSuperviewEdges()
}
}
This view controller is displayed when app is launching. This code represents the whole situation as app crashes even on this simple view -
[UILabel autoPinEdgesToSuperviewEdges]: unrecognized selector sent to instance.
Edit
Same code works on release build config. Does anyone know why?
The best thing I can suggest is start all over from the vary beginning. Depending on how much code you have this might be intimidating, but it is the best I have to offer. But my advice is this: don't move onto something else until the bug is obliterated, doing so can save you a lot of stress.

Swift 1.2 crashes with .lowercaseString

I have this relatively simple method for filtering a tableView's datasource objects.
func filterCategoriesWithQuery(query: String) {
placeCategoriesTableViewDataSource.filteredCategories
= placeCategoriesTableViewDataSource.placeCategories.filter({ (category: JSON) -> Bool in
let categoryName = (category["name"].stringValue).lowercaseString
if categoryName.hasPrefix(query.lowercaseString) {
return true
} else {
return false
}
})
genericTableView.reloadData()
This worked perfectly well prior to updating to Swift-1.2/Xcode-6.3b but now it always crashes when lowercaseString is utilised. It seems there's a bug with the lowercaseString method?
With NSZombieEnabled or breaking at malloc_free_break I can see that it crashes with [CFString release]: message sent to deallocated instance
Am I doing something wrong? Is this a bug? Any workarounds?
This is a bug in the first beta of Swift 1.2.
On the Apple Developer Forums, Chris Lattner mentioned a similar bug with uppercaseString that should be fixed in the next beta.
Until then, as a workaround, you can try to change the Swift compiler optimization to none in the build settings of your project. This helped me to get around a similar issue I had with an Array.

Swift - Define properties inside class

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 :)

Resources