read xls(Excel) file with non english character(**not xlsx file)
i have tried all the encoding option but i am unable to read xls file content.
let data = try Data(contentsOf: url)// Document Directory file path
let dataEncoded = String(data: data, encoding: .utf8)
Example
Thanks in advance.
resolved my issue through below SDK:
https://github.com/QuetzalMX/QuetzalXLSReader
Related
I'm using the following line in Swift to create a CSV file:
filePathToCSV.write(to: url, atomically: true, encoding: .utf8)
Unfortunately, the file has a MIME type of text/plain. How can I change the MIME type to text/csv?
I've searched high and low for a solution, but haven't found one yet.
Any help is much appreciated. Thank you!
Thank you everyone for your input!
I received some help from the Apple Developer Forums:
https://developers.apple.com/forums/thread/710757?page=1#721303022
Changing the line-break characters from \n to \r\n did the trick! The file command now shows the file's MIME type as text\csv.
I want to decode a base64 encoded .svg image using Swift 5.x. Can anybody guide me on how to do it?
I have encoded and decoded base64 .png before. Need help about .svg specifically.
pod 'SVGKit'
install this pod and then set up this code is really help me to solve my problem
let temp = qrCode.components(separatedBy: ",")
let dataDecoded : Data = Data(base64Encoded: temp[1], options:
.ignoreUnknownCharacters)!
let decoded = SVGKImage(data:dataDecoded)
self.youriimage.image = decoded?.uiImage
I have studied similar questions but couldnot find solution.
I am using WkWebView. It renders a html from library directory so i did loadfileurl.
It has option of displaying an image, we select the image from gallery/take camera , then from image data wecreate a file in library directory and sendthe path to web .
I tried both path var/... and also appended file://
both cases image is not displaying.
Please help.
Any suggestions appreciated.
An alternative would be to encode the images as base64 strings and insert them into the HTML before rendering. You could either save the images as images or the base64 string, depending on whether you need to use them in another context.
// Get the base64 representation of the image
let image = UIImage(named: icon) // This could also be loaded from the file system
let data = image!.pngData()
let b64String = String(data: data!.base64EncodedData(), encoding: String.Encoding.utf8)!
let finalString = "data:image/png;base64," + b64String
// Insert into HTML string before rendering in webview
let myHTML = baseHTML.replacingOccurrences(of: "#PLACEHOLDER#", with: finalString)
You will need to have #PLACEHOLDER# in your HTML file at the point where you would have the path to the image.
My app needs to read text files encoded in GB2312. Here is the current code.
SGFString = try String(contentsOf:path)
It throws an exception.
couldn’t be opened because the text encoding of its contents can’t be
determined.
While looking into the String.Encoding values, there is no GB2312. What would be the best way to read the GB2312 text files?
Thanks
Ray
Thanks for OOPer's help. I copied some of the code from the links, now it works.
Here is the code:
extension String.Encoding {
static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}
Gb2312String = try String(contentsOf:path, encoding:String.Encoding.gb_18030_2000)
This code:
var str = "ぴ" //Japanese: pi
xcode report compilation error:
Unprintable ASCII character found in source file
Have you tried this?
func convertString(string: String) -> String {
var data = string.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
return NSString(data: data!, encoding: NSASCIIStringEncoding) as! String
}
From How to deal with a user input string that gives an "unprintable ascii character found in source file" error when pasted into Xcode?
This answer is for the people who are using windows keyboard with mac system.
While typing you might have unknowingly clicked the right-click key in your keyboard, which will create the Unprintable ASCII character that is causing the Unprintable ASCII character found in the source file error.
To check this go to find(cmd+F) and click the right-click key and you can find them in your class and delete those characters and the error will be gone.
Thanks
SWIFT 4:
func convertString(string: String) -> String {
let data = string.data(using: String.Encoding.ascii, allowLossyConversion: true)
return NSString(data: data!, encoding: String.Encoding.ascii.rawValue)! as String
}
In a Playground your line won't give you an error as it is, since it uses UTF-8 encoding by default. In a regular project your trusty File inspector will allow you to define the Text Encoding in the Text Settings section. I recommend to use some version of Unicode, probably UTF-8 if your app will deal primarily with western languages and UTF-16 if you have a lot of text constants in asian languages.
This just for the record since this is an old question and you probably found a working solution years ago.