Swift with XCode 6 Beta 7 has troubles with register notifications - xcode

I am developing an application with Swift and XCode 6. Yesterday I installed XCode 6 beta 7 and a part of my code (which was already running before this install) now crashing with bad access error.
Here is the part of my code:
var types: UIUserNotificationType = UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
The crashing line is let settings = UIUserNotificationSettings(forTypes: types, categories: nil). The error is: EXC_BAD_ACCESS(code=1, address=0x20)
Does anybody has an idea about this?

Are you targeting 7.1 (or anything < 8.0)? The API you're using is 8.0 only. From the Apple docs:
#availability(iOS, introduced=8.0)
func registerUserNotificationSettings(notificationSettings: UIUserNotificationSettings)
#availability(iOS, introduced=8.0)
class UIUserNotificationSettings : NSObject {
...
}

Related

How to drag and drop an element for xcode-ui-testing in Xcode 9

UI Test for an ios-app, developed in Xcode 8 and swift 3.2.
I'm facing a problem to deal with drag and drop after upgrading Xcode version 8 to 9
I would like to drag an element [i.e. button] and drop it to another element [i.e. on homepage].
For Xcode 8, I implemented it using below method:
let sourceElement = app.buttons["Video_Button"]
let destElement = app.scrollViews.scrollViews.images.element(boundBy: 0)
sourceElement.press(forDuration: 0.5, thenDragTo: destElement)
But the above code is not working in Xcode 9 and Swift 3.2.
Actually issue is in iOS 11.0.
In iOS 11.0 some hierarchical elements failed to tap or press or longpress.
You have to work with the elements center point to do the tap or press or longpress.
** Code snippet for the Solution of the problem is pasted below.
First create an extension method for press on a center of the element and drag this element to the destination elements center.
Extension method is below
extension XCUIElement {
func dragAndDropUsingCenterPos(forDuration duration: TimeInterval,
thenDragTo destElement: XCUIElement) {
let sourceCoordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))
let destCorodinate: XCUICoordinate = destElement.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))
sourceCoordinate.press(forDuration: duration, thenDragTo: destCorodinate)
}
}
Then call the extension method like below
sourceElement.dragAndDropUsingCenterPos(forDuration: 0.5, thenDragTo: destElement)
Hope it will solve your issue. Let me know your feedback.

Swift ios9 Building MacinTalk voice for asset: (null)

I am using xcode 7 , swift 2.0
I am getting voice text to Speech working in Simulator but not in real iphone6 plus device, iOS 9. I have properly imported AVFOUNDATION and its Framework.
I tried...
#IBAction func SpeakTheList(sender: AnyObject) {
let mySpeechUtterance = AVSpeechUtterance(string: speakString)
//let voice = AVSpeechSynthesisVoice(language: "en-US")
// mySpeechUtterance.voice = voice
let voices = AVSpeechSynthesisVoice.speechVoices()
for voice in voices {
if "en-US" == voice.language {
mySpeechUtterance.voice = voice
print(voice.language)
break;
}
}
mySpeechSynthesizer.speakUtterance(mySpeechUtterance)
}
I get the following error :
Building MacinTalk voice for asset: (null)
Is there anything I ned to do settings in my iphone6plus iOS 9 , or I have to download something.
I have found a suggestion here Why I'm getting "Building MacinTalk voice for asset: (null)" in iOS device test
saying that..
" since iOS9, possibly a log event turned on during development that they forgot to turn off"
Just want to add to this (and by extension, the linked discussion in the original post):
I have two devices: an iPad2 and an iPad Air. They are running exactly the same version of iOS (9.2, 13C75). I have the following objective-C++ function for generating speech from Qt using Xcode 7.2 (7C68) on Yosemite:
void iOSTTSClient::speakSpeedGender(const QString &msg, const float speechRateModifier, const QString &gender, const bool cutOff) {
QString noHTML(msg);
noHTML.remove(QRegularExpression("<[^<]*?>"));
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:noHTML.toNSString()];
/* See https://forums.developer.apple.com/thread/18178 */
const float baseSpeechRate = (m_iOSVersion < 9.0) ? 0.15 : AVSpeechUtteranceDefaultSpeechRate;
utterance.rate = baseSpeechRate * speechRateModifier;
NSString *locale;
if (gender.compare("male", Qt::CaseInsensitive) == 0)
locale = #"en-GB"; // "Daniel" by default
else if (gender.compare("female", Qt::CaseInsensitive) == 0)
locale = #"en-US"; // "Samantha" by default
else
locale = [AVSpeechSynthesisVoice currentLanguageCode];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:locale];
const QString errMsg = QString("Null pointer to AVSpeechSynthesisVoice (could not fetch voice for locale '%1')!").arg(QString::fromNSString(locale));
Q_ASSERT_X(voice, "speakSpeedGender", errMsg.toLatin1().data());
utterance.voice = voice;
static const AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
if (synthesizer.speaking && cutOff) {
const bool stopped = [synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
Q_ASSERT_X(stopped, "speakSpeedGender", "Could not stop previous utterance!");
}
[synthesizer speakUtterance:utterance];
}
On the iPad Air, everything works beautifully:
Building MacinTalk voice for asset:
file:///private/var/mobile/Library/Assets/com_apple_MobileAsset_MacinTalkVoiceAssets/db2bf75d6d3dbf8d4825a3ea16b1a879ac31466b.asset/AssetData/
But on the iPad2, I hear nothing and get the following:
Building MacinTalk voice for asset: (null)
Out of curiosity, I fired up the iPad2 simulator and ran my app there. I got yet another console message:
AXSpeechAssetDownloader|error| ASAssetQuery error fetching results
(for com.apple.MobileAsset.MacinTalkVoiceAssets) Error Domain=ASError
Code=21 "Unable to copy asset information"
UserInfo={NSDescription=Unable to copy asset information}
However, I heard speech! And I realized I was wearing headphones. Sure enough, when I plugged ear buds into the iPad2, I heard speech there too. So now I'm searching for information about that. The following link is recent and has the usual assortment of this-worked-for-me voodoo (none of it helped me, but maybe will help others with this problem):
https://forums.developer.apple.com/thread/18444
In summary: TTS "works" but is not necessarily audible without headphones/ear buds. It appears to be a hardware settings issue with iOS 9.2. The console messages may or may not be relevant.
Final update: in the interests of full, if sheepish, disclosure, I figured I'd share how I finally solved the issue. The iPad2 in question had the "Use side switch to:" option set to "Mute". I left that alone but went ahead and toggled the switch itself. Wham! Everything worked without ear buds. So if you are unable to hear text-to-speech, try ear-buds. If that works, check whether your device is set to mute!
Do not use pauseSpeakingAtBoundary(). Instead, use stopSpeakingAtBoundary and continueSpeaking. This works for me.
Finally Found that there was a bug in iOS9, soon after XCODE new release 7.2 update, and iOS 9.2 Update release,
I tested same above code, text to speech started working.

Swift Error: Cannot find an initializer for type 'Double' that accepts an argument list of type '(String)'

I am trying to grab data from a text field labeled 'temperatureTextField' and assigning it to 't' which is a Double. Ideally the user is meant to add a number value to the temperatureTextField.
Here is my method:
#IBOutlet weak var temperatureTextField: UITextField!
#IBAction func convert(sender: AnyObject) {
let t = Double(temperatureTextField.text!)
let tempM = TemperatureModel(temp: t!)
temperatureTextField.text = String(tempM.toCelsius())
}
The red exclamation is coming from the line "let t = Double(temperatureTex...)"
You're probably using Xcode 6, so Swift 1.2, but the String initializer for Double is only available in Swift 2 (Xcode 7).
You can always use NSString's doubleValue property:
let t = (temperatureTextField.text! as NSString).doubleValue
but I'd recommend using Xcode 7 and Swift 2 as soon as possible.
As Eric suggested, I ran into this issue because I was running an outdated version of xcode.
Here is what my code looked liked after, in case anyone runs into trouble and is unable to update:
let t = (inputText.text! as NSString).doubleValue
let tempModel = TemperatureModel(temp: t)
inputText.text = "\(tempModel.toCelsius())"

XCode 7 beta 2 String is not convertible to StringLiteralConvertible

I was doing a playground to check swift 2.0 and this happens:
I do not know if im missing something, or is this normal or what? Thanks.
In swift 1.2 is working as expected XCode 6.3.
EDIT: Code
//: Playground - noun: a place where people can play
import Foundation
var str = "Hello, playground"
let languageType: String = "Swift"
var version = 1.0 //infered
let introduced = 2014 //infered
let isAwesome = true //infered
let π = 3.1415927
let 🐶🐮 = "dogcow"
let someString = "I appear to be a string"
let pathComponent = "~/Documents/Swift".pathComponents
var s = String("bla vla nla")
for character in "catDog" {
print(character)
}
the API for String has changed in Swift 2, to accomplish what it looks like you are trying to do, you would instead use for character in "catDog".characters {...}
an excellent reference is Nate Cook's swiftdoc.org

Xcode 7: Can't use CalendarUnitMonth in Swift 2.0, Xcode 7

I am using swift2.
I used this code:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitMonth | .CalendarUnitDay, fromDate: theDate)
I am getting this error: Could not find member 'CalendarUnitMonth'
Check Swift 2 documentation and search for OptionSetType (adopted by NSCalendarUnit). You can use it in this way:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Month, .Day], fromDate:NSDate())

Resources