Xcode - Swift 4 Encoding error - xcode

I have this code :
let data = "NgAzADYANQA1ADEANwA0ADgANQA1ADQANgA4ADgAMAA0ADcALwAvAGIAYQAwAGQAZABlAGQANAAtAGYANAAzAGUALQA0ADAANABkAC0AYQAzAGYAYgAtADQAZQA2ADIAZQBhADkAMgBiADMAYgBiAA=="
let dataDecoded = Data(base64Encoded: data, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!
let decodedString = String(decoding: dataDecoded, as: UTF8.self)
print(decodedString)
On my output window, i have this result :
636551748554688047//ba0dded4-f43e-404d-a3fb-4e62ea92b3bb
But on my variable, i have :
6\03\06\05\05\01\07\04\08\05\05\04\06\08\08\00\04\07\0/\0/\0b\0a\00\0d\0d\0e\0d\04\0-\0f\04\03\0e\0-\04\00\04\0d\0-\0a\03\0f\0b\0-\04\0e\06\02\0e\0a\09\02\0b\03\0b\0b\0
please help me :)

Seems your input data is not int UTF-8, try this:
let decodedString = String(data: dataDecoded, encoding: .utf16LittleEndian)

Related

imageSourceModule.fromFile(path) returns null in nativescript

Code is:
let folder = fileSystemModule.knownFolders.currentApp();
let path = fileSystemModule.path.join(folder.path,img.src.android);
let abc = imageSourceModule.fromPath(path);

Convert SQL Statement to Predicate or NSExpression

I am facing a problem with converting below SQL to NSExpression
SELECT id,trait1,trait2,trait3,sum(trait1+trait2+trait3) as total FROM `bookings` GROUP BY id order by total desc
Below is my code in this code I am only able to add the two key values not three key values.
func aggregateProductsInContext(context:NSManagedObjectContext) {
var expressionDescriptions = [AnyObject]()
expressionDescriptions.append("name")
let expressionDescription = NSExpressionDescription()
expressionDescription.name = "Sold"
let exp1 = NSExpression(forKeyPath: "trait1")
let exp3 = NSExpression(forKeyPath: "trait2")
let exp3 = NSExpression(forKeyPath: "trait3")
let stas = NSExpression(forFunction:"add:to:", arguments:[exp1,exp3])
expressionDescription.expression = stas
expressionDescription.expressionResultType = .Integer64AttributeType
expressionDescriptions.append(expressionDescription)
let request = NSFetchRequest(entityName: "booking")
request.propertiesToGroupBy = ["name"]
request.resultType = .DictionaryResultType
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
request.propertiesToFetch = expressionDescriptions
var results:[[String:AnyObject]]?
do {
results = try context.executeFetchRequest(request) as? [[String:AnyObject]]
} catch _ {
results = nil
}
print(results)
}
Please find the attached images of actual table values and resulted values for reference.
Thanks!
My Solution:
I am able to solve my problem by doing this
let ec = NSExpression(forFunction:"add:to:", arguments:[exp1,exp2])
let ec1 = NSExpression(forFunction:"add:to:", arguments:[ec,exp3])
let ec2 = NSExpression(forFunction:"add:to:", arguments:[ec1,exp4])
let ec3 = NSExpression(forFunction:"add:to:", arguments:[ec2,exp5])
If there is any other way to achieve this, please let me know
Thanks!
Here is the solution which I had implemented and it's work fine.
let ec = NSExpression(forFunction:"add:to:", arguments:[exp1,exp2])
let ec1 = NSExpression(forFunction:"add:to:", arguments:[ec,exp3])
let ec2 = NSExpression(forFunction:"add:to:", arguments:[ec1,exp4])
let ec3 = NSExpression(forFunction:"add:to:", arguments:[ec2,exp5])
But I am sure there must be some other good way to do that. If any one have any other solution. please let me know.
Thanks!

Swift 2: expression is ambiguous?

The following code used to work before I migrated to Swift 2 now I can't seem to work around it:
let cal = NSCalendar(calendarIdentifier: GregorianCalendar)
let components = cal!.components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: date) **error at this line**
let newDate = cal!.dateFromComponents(components)
I'm getting the following error message:
Type of expression is ambiguous without more context
In Swift 2 there is a change in the declaration of NSCalendarUnit as well as in the handling of OptionSetType
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let components = cal!.components([.Day, .Month, .Year], fromDate: date)
Please folks read the Swift Blog and the current Swift Language Guide
I was able to fix the error by force typecasting
let components = calendar.components([.Hour, .Minute, .Second], fromDate: date as! NSDate)

Swift 2 - kCTForegroundColorAttributeName

let nameAttributes = [NSFontAttributeName:nameFont, kCTForegroundColorAttributeName:UIColor.whiteColor().CGColor] as [String:AnyObject]
var nameAttributedString = NSMutableAttributedString(string:name, attributes:nameAttributes)
I have these values which are working on Swift 1.2. But on Swift 2, they don't work.
I receive an error on first line:
'_' is not convertible to 'CFString'
And the problem is kCTForegroundColorAttributeName. Without kCTForegroundColorAttributeName, it would works. But I need it to change the color..
In addition:
kCTForegroundColorAttributeName:UIColor.whiteColor().colorWithAlphaComponent(0.7).CGColor
gives me an error:
'_' is not convertible to 'CGFloat'
In Swift 2, attributes on NSMutableAttributedString has to be [String:AnyObject] while in Swift 1.2 was [NSObject:AnyObject].
Any ideas ?
Why not just use NSForegroundColorAttributeName?
Use this Code
let attrs2 = [NSFontAttributeName : FontWithBook(10),NSForegroundColorAttributeName :UIColor.grayColor()]
var gString = NSMutableAttributedString(string:"(Mandatory)", attributes:attrs2)

Use linq to query blocks of text?

I have a text file that contains some data in a "block" format:
source : source location
filename : somefile.txt
vendor : somevendor
version : xx.xx.xxx
source : source location2
filename : somefile2.txt
vendor : somevendor2
version : yy.yy.yyy
can I use Linq to query this data and if so how would you go about it? I have used linq to query lines of data from text file many times, but never a "block" of data as above. Thanks for the input.
Yes, you can use LINQ, this approach is not optimized much if you have large file. Below is how to get data:
var lines = File.ReadLines("C:\\text.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.ToList();
for (int i = 0; i < lines.Count; i += 4)
{
var location = lines[i].Split(':')[1];
var fileName = lines[i + 1].Split(':')[1];
var vendor = lines[i + 2].Split(':')[1];
var version = lines[i + 3].Split(':')[1];
}
Version to use LINQ:
var result = Enumerable.Range(0, lines.Count()/4).Select(i => new {
location = lines[4*i].Split(':')[1];
fileName = lines[4*i + 1].Split(':')[1];
vendor = lines[4*i + 2].Split(':')[1];
version = lines[4*i + 3].Split(':')[1];
});

Resources