public convenience init(nsurl:NSURL) {
var enc:NSStringEncoding = NSUTF8StringEncoding
var err:NSError?
let str:String? =
NSString(
contentsOfURL:nsurl, usedEncoding:&enc, error:&err
)
if err != nil { self.init(err!) }
else { self.init(string:str!) }
}
Swift is version 1.2, error message is:
NSString? is not convertible to string
With Swift 1.2 automatic bridging between String and NSString has been removed.
So you have to explicitly do the cast :
let str = NSString(contentsOfURL:nsurl, usedEncoding:&enc, error:&err) as? String
Swift's String accepts this same initializer as NSString, so you don't even have to use NSString nor to typecast:
let str = String(contentsOfURL: nsurl, encoding: enc, error: &err)
Update for Swift 2.0
do {
let str = try String(contentsOfURL: nsurl, encoding: enc)
print(str)
} catch {
print(error)
}
You can also use an Optional with try? if you want, in this case no need for do catch:
if let str = try? String(contentsOfURL: nsurl, encoding: enc) {
print(str)
} else {
// didn't succeed
}
In Swift 3 I am using the following
do{
let str = try String(contentsOf:personUrl!)
print(str)
}catch let error{
print(error)
}
Related
I am trying to fetch some record from some entity , but when trying to fetch frequently i am getting Bad Access error ,and app is crashing . please help .
var mContext:NSManagedObjectContext! = appDelegate.persistentContainer.viewContext
func getAllRoomName() -> [String] {
let fetchRequest: NSFetchRequest<SwitchMO> = SwitchMO.fetchRequest()
var arrRoomNames = [String]()
do {
if let arrSwitchesMo = try? mContext.fetch(fetchRequest) as? [SwitchMO]
{
for switchMo in arrSwitchesMo ?? []
{
arrRoomNames.append(switchMo.roomName ?? "")
}
}
} catch {
print("Error with request: \(error)")
}
arrRoomNames = Array(Set(arrRoomNames))
return arrRoomNames;
}
Bad Access Error
How can i get rid of this , Please help me .
If you are using a specific fetch request a type cast is redundant. And if you are using do catch don't try?
func getAllRoomName() -> [String] {
let fetchRequest: NSFetchRequest<SwitchMO> = SwitchMO.fetchRequest()
var arrRoomNames = [String]()
do {
let arrSwitchesMo = try mContext.fetch(fetchRequest)
for switchMo in arrSwitchesMo {
arrRoomNames.append(switchMo.roomName ?? "")
}
arrRoomNames = Array(Set(arrRoomNames))
} catch {
print("Error with request: \(error)")
}
return arrRoomNames
}
However you should make a function can throw if this function on its part contains a throwing function
func getAllRoomName() throws -> [String] {
let fetchRequest: NSFetchRequest<SwitchMO> = SwitchMO.fetchRequest()
var arrRoomNames = [String]()
let arrSwitchesMo = try mContext.fetch(fetchRequest)
for switchMo in arrSwitchesMo {
arrRoomNames.append(switchMo.roomName ?? "")
}
return Array(Set(arrRoomNames))
}
If the code still crashes then the managed object context is nil. Declare the context non-optional as suggested in the Core Data template.
I am very new to Swift.
I want to create something like API on Swift for my educational app.
I have this code:
static func getFilm(filmID: Int) -> String {
print("getFilm")
let url = URL(string: "https://api.kinopoisk.cf/getFilm?filmID=\(filmID)")!
var request = URLRequest(url: url)
var returnData: String = ""
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if var responseVar = response, var dataVar = data {
print(responseVar)
returnData = String(data: dataVar, encoding: .utf8)
} else {
print(error)
}
}
task.resume()
return returnData
}
And I try to convert Data to String in this line: returnData = String(data: dataVar, encoding: .utf8)
Swift compiler gives me an error, and change this line to
returnData = String(data: dataVar, encoding: .utf8)!
, when I execute this line I get empty returnData variable.
If I use basic example line
print(String(data: data, encoding: .utf8))
everything will be OK and I can see data in XCode console.
So, how I can convert Data to String?
This is an example using a completion handler:
class func getFilm(filmID: Int, completion: #escaping (String) -> ()) {
let url = URL(string: "https://api.kinopoisk.cf/getFilm?filmID=\(filmID)")!
URLSession.shared.dataTask(with:url) { (data, response, error) in
if error != nil {
print(error!)
completion("")
} else {
if let returnData = String(data: data!, encoding: .utf8) {
completion(returnData)
} else {
completion("")
}
}
}.resume()
}
And you call it
MyClass.getFilm(filmID:12345) { result in
print(result)
}
In case of an error the completion handler returns an empty string.
MyClass is the enclosing class of getFilm method. Most likely the web service will return JSON, so you might need to deserialize the JSON to an array or dictionary.
In a more sophisticated version create an enum with two cases and associated values
enum ConnectionResult {
case success(String), failure(Error)
}
With a little more effort demonstrating the subtle power of Swift you can return either the converted string on success of the error on failure in a single object.
class func getFilm(filmID: Int, completion: #escaping (ConnectionResult) -> ()) {
let url = URL(string: "https://api.kinopoisk.cf/getFilm?filmID=\(filmID)")!
URLSession.shared.dataTask(with:url) { (data, response, error) in
if error != nil {
completion(.failure(error!))
} else {
if let returnData = String(data: data!, encoding: .utf8) {
completion(.success(returnData))
} else {
completion(.failure(NSError(domain: "myDomain", code: 9999, userInfo: [NSLocalizedDescriptionKey : "The data is not converible to 'String'"])))
}
}
}.resume()
}
On the caller side a switch statement separates the cases.
MyClass.getFilm(filmID:12345) { result in
switch result {
case .success(let string) : print(string)
case .failure(let error) : print(error)
}
}
I had this problem, you can't use encoding: .utf8 for unpredictable data. It will return nil every time.
Use this instead:
String(decoding: data, as: UTF8.self)
For anyone coming in future (which are probably not interested in OP's film code?!);
Simply, try something like:
extension Data {
public func toString() -> String {
return String(data: self, encoding: .utf8) ?? "";
}
}
See also my toHex related answer
I need to know if a string contains an Int to be sure that a name the user entered is a valid full name,
for that I need to either make the user type only chars, or valid that there are no ints in the string the user entered.
Thanks for all the help.
You can use Foundation methods with Swift strings, and that's what you should do here. NSString has built in methods that use NSCharacterSet to check if certain types of characters are present. This translates nicely to Swift:
var str = "Hello, playground1"
let decimalCharacters = CharacterSet.decimalDigits
let decimalRange = str.rangeOfCharacter(from: decimalCharacters)
if decimalRange != nil {
print("Numbers found")
}
If you're interested in restricting what can be typed, you should implement UITextFieldDelegate and the method textField(_:shouldChangeCharactersIn:replacementString:) to prevent people from typing those characters in the first place.
Simple Swift 4 version using rangeOfCharacter method from String class:
let numbersRange = stringValue.rangeOfCharacter(from: .decimalDigits)
let hasNumbers = (numbersRange != nil)
This method is what i use now for checking if a string contains a number
func doStringContainsNumber( _string : String) -> Bool{
let numberRegEx = ".*[0-9]+.*"
let testCase = NSPredicate(format:"SELF MATCHES %#", numberRegEx)
let containsNumber = testCase.evaluateWithObject(_string)
return containsNumber
}
If your string Contains a number it will return true else false. Hope it helps
//Swift 3.0 to check if String contains numbers (decimal digits):
let someString = "string 1"
let numberCharacters = NSCharacterSet.decimalDigits
if someString.rangeOfCharacter(from: numberCharacters) != nil
{ print("String contains numbers")}
else if someString.rangeOfCharacter(from: numberCharacters) == nil
{ print("String doesn't contains numbers")}
//A function that checks if a string has any numbers
func stringHasNumber(_ string:String) -> Bool {
for character in string{
if character.isNumber{
return true
}
}
return false
}
/// Check stringHasNumber function
stringHasNumber("mhhhldiddld")
stringHasNumber("kjkdjd99900")
if (ContainsNumbers(str).count > 0)
{
// Your string contains at least one number 0-9
}
func ContainsNumbers(s: String) -> [Character]
{
return s.characters.filter { ("0"..."9").contains($0)}
}
Swift 2.3. version working.
extension String
{
func containsNumbers() -> Bool
{
let numberRegEx = ".*[0-9]+.*"
let testCase = NSPredicate(format:"SELF MATCHES %#", numberRegEx)
return testCase.evaluateWithObject(self)
}
}
Usage:
//guard let firstname = textField.text else { return }
let testStr1 = "lalalala"
let testStr2 = "1lalalala"
let testStr3 = "lal2lsd2l"
print("Test 1 = \(testStr1.containsNumbers())\nTest 2 = \(testStr2.containsNumbers())\nTest 3 = \(testStr3.containsNumbers())\n")
You need to trick Swift into using Regex by wrapping up its nsRegularExpression
class Regex {
let internalExpression: NSRegularExpression
let pattern: String
init(_ pattern: String) {
self.pattern = pattern
var error: NSError?
self.internalExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)
}
func test(input: String) -> Bool {
let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
return matches.count > 0
}
}
if Regex("\\d/").test("John 2 Smith") {
println("has a number in the name")
}
I got these from http://benscheirman.com/2014/06/regex-in-swift/
let numericCharSet = CharacterSet.init(charactersIn: "1234567890")
let newCharSet = CharacterSet.init(charactersIn: "~`##$%^&*(){}[]<>?")
let sentence = "Tes#ting4 #Charact2er1Seqt"
if sentence.rangeOfCharacter(from: numericCharSet) != nil {
print("Yes It,Have a Numeric")
let removedSpl = sentence.components(separatedBy: newCharSet).joined()
print(sentence.components(separatedBy: newCharSet).joined())
print(removedSpl.components(separatedBy: numericCharSet).joined())
}
else {
print("No")
}
Whenever I do println(error.localizedDescription) I get something that says :
Optional("description of the error here")
Rather than just:
"description of the error here"
How do I get rid of the Optional() part of the description?
I tried the below method which results in a compiler error saying that it's not an optional.
func performLoginRequestWithURL(url: NSURL, email: String, password: String) {
let bodyData = "email=\(email)&password=\(password)"
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
response, data, error in
if let error = error {
let errString = error.localizedDescription
NSNotificationCenter.defaultCenter().postNotificationName(self.lh, object: nil, userInfo: ["Result": errString])
} else if data != nil {
let json = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
if let dictionary = JSON().parseJSON(json) as [String: AnyObject]? {
let accesstoken = dictionary["id"] as! String
let id = dictionary["userId"] as! Int
var results = [String: AnyObject]()
results = ["at": accesstoken, "id": id]
// MARK: - Store UID & AccessToken
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "userLoggedIn")
NSUserDefaults.standardUserDefaults().setInteger(id, forKey: "userId")
NSUserDefaults.standardUserDefaults().setObject(accesstoken, forKey: "accessToken")
NSUserDefaults.standardUserDefaults().synchronize()
NSNotificationCenter.defaultCenter().postNotificationName(self.lh, object: nil, userInfo: ["Result": "Success"])
}
}
}
}
The compiler is correct - error is an optional but error.localizedDescription is not. You either need to unwrap the error first, safely by
if let unwrappedError = error {
println(unwrappedError.localizedDescription)
}
or: Use optional chaining - https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html
let description = error?.localizedDescription
which will either cause 'description' to be the string of localizedDescription, if the error optional can be unwrapped successfully, or possibly 'nil' if not.
Code taken from example added to question. Seems to work as ok, below code can be copied into playground to show error and data string printout - just add valid http:// address into see valid return or try with just 'http:' to get an error.
import UIKit
import XCPlayground
func performLoginRequestWithURL(url: NSURL) {
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
response, data, error in
if let error = error {
let errString = error.localizedDescription
print(errString)
} else if data != nil {
let json = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print(json)
}
}
}
performLoginRequestWithURL(NSURL(string:"http:")!)
XCPSetExecutionShouldContinueIndefinitely()
You have to unwrap error using ?:
if let errString = error?.localizedDescription {
println(errString)
}
If you already know from an earlier check that error isn't nil, you can force unwrap:
println(error!.localizedDescription)
This will crash if error is nil, so use the ! syntax with caution.
Well I am trying to creating a simple tool to read an specific offset address from a file that's within the project.
I can read it fine and get the bytes, the problem is that I want to convert the result into a string, but I just can't.
My output is this: <00000100 88000d00 02140dbb 05c3a282> but I want into String.
Found some examples of doing it using an extension for NSData, but still didn't work.
So anyone could help??
Here's my code:
class ViewController: UIViewController {
let filemgr = NSFileManager.defaultManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let pathToFile = NSBundle.mainBundle() .pathForResource("control", ofType: "bin")
let databuffer = filemgr.contentsAtPath(pathToFile!)
let file: NSFileHandle? = NSFileHandle(forReadingAtPath: pathToFile!)
if file == nil {
println("File open failed")
} else {
file?.seekToFileOffset(197584)
let byte = file?.readDataOfLength(16)
println(byte!)
file?.closeFile()
}
}
}
So long as you know the encoding, you can create a string from an NSData object like this
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
By the way, you might want to try using if let to unwrap your optionals rather than force-unwrapping, to account for failure possibilities:
let filemgr = NSFileManager.defaultManager()
if let pathToFile = NSBundle.mainBundle() .pathForResource("control", ofType: "bin"),
databuffer = filemgr.contentsAtPath(pathToFile),
file = NSFileHandle(forReadingAtPath: pathToFile)
{
file.seekToFileOffset(197584)
let bytes = file.readDataOfLength(16)
let str = NSString(data: bytes, encoding: NSUTF8StringEncoding)
println(str)
file.closeFile()
}
else {
println("File open failed")
}
The correct answer following #Martin R suggestion to this link: https://codereview.stackexchange.com/a/86613/35991
Here's the code:
extension NSData {
func hexString() -> String {
// "Array" of all bytes:
let bytes = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count:self.length)
// Array of hex strings, one for each byte:
let hexBytes = map(bytes) { String(format: "%02hhx", $0) }
// Concatenate all hex strings:
return "".join(hexBytes)
}
}
And I used like this:
let token = byte.hexString()