Operations based on user input Swift - cocoa-touch

long question, so bear with me...
I am attempting to create a bitcoin ticker and converter written in Swift. I am using the code below (bits not related to conversion are removed - let me know if I left out anything important)
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var USD: UITextField! //Amount of USD. Originally equals 1 bitcoin, can be changed by user.
#IBOutlet weak var BTC: UILabel! //Amount of bitcoins the entered amount of USD is worth. Originally 1.
func handler(response: NSURLResponse!, data : NSData!, error : NSError!) { //To fetch Bitcoin Price. This is functional.
if ((error) != nil) {
self.USD.text = "No Internet" // in case of error
} else {
var price = NSString(data:data, encoding:NSUTF8StringEncoding)
self.USD.text = price //set USD to be equal to price of 1 Bitcoin
}
override func viewDidLoad() {
//Sets up view
self.update() //Fetches Bitcoin Price. This works.
self.convert() //Begins convert method
var timer = NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func convert() {
var url = NSURL(string:"https://api.bitcoinaverage.com/ticker/USD/last")
var request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:handler)
var data = NSData(contentsOfURL:url);
while true {
if USD.text != data {
BTC.text = USD.text / NSString(data:data, encoding:NSUTF8StringEncoding)// Attempts to determine amount of bitcoin the USD amount is worth. ERROR HERE!
}
}
On the line with the comment "ERROR HERE", I get the error
/Users/[My User]/Documents/dev/Newest Stuff/CryptoCoinTicker/CryptoCoinTicker/ViewController.swift:95:32: 'String' is not convertible to 'UInt8'
In case the code doesn't tell the story, I want BTC.text to change to be equal in value to the amount entered by the user in USD.text (so if a bitcoin is worth $500, and the user entered 250.00, BTC.text would say 0.5.
What do I need to do? Apologies for a (probably) basic question, I am but a newbie. Let me know if you ned more info.
Thanks in advance!

When you get that error, it usually means that you are trying to assign a wrong type to a variable or that you are using the wrong types for function parameters.
In your case, you are trying to divide two Strings. The compiler doesn't know what to do, since the division of Strings is not defined. You can only divide Int, Float and Double and you can't even mix them!
So for that line you can substitute this:
let value = NSString(string: USD.text).doubleValue / NSString(data:data, encoding:NSUTF8StringEncoding).doubleValue
BTC.text = "\(value)"
This first "converts" the USD.text to NSString and then converts both NSStrings to Doubles, which you can then divide.
It then creates a String from that value, which can be assigned to the BTC.text property.

Related

Unable to understand the certain operator in Swift

I have been changing some SWIFT code into OBJECTIVE-C, and I am stuck at certain part of the code, where I am unable to understand if it is a condition or something else.
Following is the code and I am stuck on 9th line stating :
if let channel1Buffer = buffer.floatChannelData?[0]
What I do not understand here is the above if condition is checking if "buffer.floatChannelData" is null, and then proceeding to get the first index, or is it something else.
input.installTap(onBus: 0, bufferSize:4096, format:format, block: { [weak self] buffer, when in
guard let this = self else {
return
}
print("Buffer Float Channel Data: ", buffer.floatChannelData as Any);
**if let channel1Buffer = buffer.floatChannelData?[0]** {
print("channel1Buffer: ", channel1Buffer);
/// encode PCM to mp3
let frameLength = Int32(buffer.frameLength) / 2;
print("frameLength: ", frameLength);
let bytesWritten = lame_encode_buffer_interleaved_ieee_float(this.lame, channel1Buffer, frameLength, this.mp3buf, 4096);
// `bytesWritten` bytes stored in this.mp3buf now mp3-encoded
print("\(bytesWritten) encoded");
this.file.append(this.mp3buf, length: Int(bytesWritten));
// #TODO: send data, better to pass into separate queue for processing
}
})
Let's take it part by part - buffer.floatChannelData?[0]
buffer has property named floatChannelData which is optional so it has ? at the end. then it takes that optional which accepts subscription [0] which also returns optional value. So it continues inside {} only if floatChannelData is not nil AND it's first value is not nil
Your Objc should look like
float *const *channelData = [buffer floatChannelData];
if (channelData) {
float *channel1Buffer = channelData[0]; //this might crash if channelData is empty
...
The line tries to assign the variable channel1Buffer the value of buffer.floatChannelData[0], and the code within {} is only executed if that assignment is successful. It may for instance fail if buffer.floatChannelData is nil or buffer.floatChannelData[0] is nil.

Swift ReverseGeocode CLPlacemark areasOfInterest almost always nil. Should I be using something else?

I am using CLGeocoder and reverseGeocodeLocation to get my CLPlacemark. How come the name mostly comes back as an address and areasOfInterest come back as nil? For example... the areasOfInterest appear for really major things like apple HQ and airports, things of that sort but stores such as Walmart, Publix blah blah are nil. Should I be searching another way? Am I expecting more information than is available with this method? I mean, Apple has these points of interest on their maps, is there another way I should be trying to get this information?
Here are a few location lat longs that I've tried in my area that aren't bringing back the store names. These came from google and when put into apple maps, it beings you right on top of the correct location but it doesn't associate either... This makes me think I should be doing something different to being back the name of the store. Other info like a description or category would be nice as well.
Note: I am only wanting the information, I am not trying to place it on a map or anything.
Walmart: 35.0944° N, 85.3319° W
Aquarium: 35.0558° N, 85.3111° W
Publix: 35.0651° N, 85.3083° W
Small bit of my code. All works just wanted to give you an adea of what im bringing back and how.
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if placemarks != nil
{
if error == nil && placemarks!.count >= 1 {
let thePlacemarks = placemarks![0] as CLPlacemark
print(placemarks)
print(thePlacemarks.areasOfInterest?.description)
print(thePlacemarks.administrativeArea?.description)
print(thePlacemarks.areasOfInterest?.description)
print(thePlacemarks.country?.description)
print(thePlacemarks.inlandWater?.description)
print(thePlacemarks.isoCountryCode?.description)
print(thePlacemarks.locality?.description)
print(thePlacemarks.location?.description)
print(thePlacemarks.name?.description)
print(thePlacemarks.ocean?.description)
print(thePlacemarks.subAdministrativeArea?.description)
print()
}
}
})
Any help would be great!
Thanks!
So... Not necessarily ideal but by using mapkit I was able to do a MKLocalSearch to get what I wanted. This only works because I have an array in my code of the specific locations I am interested in. See me code below.
Import Mapkit
let listArr = ["Walmart", "Publix","Game Stop"]
Then somewhere in your ViewController
func searchArr() //This function will iterate through the array and see if any of the locations are within 30 meters
{
for element in listsArr
{
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "\(element)"
request.region = MKCoordinateRegionMakeWithDistance(currentCoordinates, 3200, 3200)
MKLocalSearch(request: request).start { (response, error) in
guard error == nil else {print()return}
guard let response = response else {return}
guard response.mapItems.count > 0 else {return}
print(response.mapItems[0])
let coord1 = currentCoordinates
let coord2 = response.mapItems[0].placemark.coordinate
let distance = self.calculateDistance(fromlat: currentCoordinates.latitude, fromlon: currentCoordinates.longitude, tolat: response.mapItems[0].placemark.coordinate.latitude, tolon: response.mapItems[0].placemark.coordinate.longitude)
if distance > 30
{
print("the distance between the two points is: \(distance) meters")
}
}
}
Here is a little function I found to get the distance between two coordinates.
func calculateDistance(fromlat : Double, fromlon : Double, tolat : Double, tolon : Double) -> Double {
let DEG_TO_RAD = 0.017453292519943295769236907684886
let EARTH_RADIUS_IN_METERS = 6372797.560856
let latitudeArc : Double = (fromlat - tolat) * DEG_TO_RAD
let longitudeArc : Double = (fromlon - tolon) * DEG_TO_RAD
var latitudeH : Double = sin(latitudeArc * 0.5)
latitudeH *= latitudeH
var lontitudeH : Double = sin(longitudeArc * 0.5)
lontitudeH *= lontitudeH
let tmp : Double = cos(fromlat*DEG_TO_RAD) * cos(tolat*DEG_TO_RAD)
return EARTH_RADIUS_IN_METERS * 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH))
}

How to convert Swift array into CFArray?

I'm trying to capture a window list in a Mac OS X app using Swift. The CGWindowListCreateImageFromArray function requires a CFArray. I've tried several things and this is the closest I've got. Or is there a better way to convert the array?
import Cocoa
// Example swift array of CGWindowID's
var windowIDs = [CGWindowID]();
windowIDs.append(1);
windowIDs.append(2);
// Convert to CFArray using CFArrayCreate
let allocator = kCFAllocatorDefault
let numValues = windowIDs.count as CFIndex
let callbacks: UnsafePointer<CFArrayCallBacks> = nil
var values: UnsafeMutablePointer<UnsafePointer<Void>> = nil
/* how do I convert windowIDs to UnsafeMutablePointer<UnsafePointer<Void>> for the values? */
let windowIDsCFArray = CFArrayCreate(allocator, values, numValues, callbacks);
let capture = CGWindowListCreateImageFromArray(CGRectInfinite, windowIDsCFArray, CGWindowImageOption(kCGWindowListOptionOnScreenOnly));
You can initialize your UnsafeMutablePointer with your array so long as you set your CGWindowIDs to CFTypeRef:
var windows: [CFTypeRef] = [1, 2]
var windowsPointer = UnsafeMutablePointer<UnsafePointer<Void>>(windows)
var cfArray = CFArrayCreate(nil, windowsPointer, windows.count, nil)
Converted Ian's answer to Swift 4:
let windows = [CGWindowID(17), CGWindowID(50), CGWindowID(59)]
let pointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: windows.count)
for (index, window) in windows.enumerated() {
pointer[index] = UnsafeRawPointer(bitPattern: UInt(window))
}
let array: CFArray = CFArrayCreate(kCFAllocatorDefault, pointer, windows.count, nil)
let capture = CGImage(windowListFromArrayScreenBounds: CGRect.infinite, windowArray: array, imageOption: [])!
let image: NSImage = NSImage(cgImage: capture, size: NSSize.zero)
Swift.print(image)
Arrays in Swift are bridged to NSArray, given they contain objects, e.g., conform to [AnyObject] type. Since CGWindowID is a UInt32, you need to convert it to NS family, array's map() method is an elegant approach.
var windows: [CGWindowID] = [CGWindowID(1), CGWindowID(2)]
var array: CFArray = windows.map({NSNumber(unsignedInt: $0)}) as CFArray
This, however, doesn't reflect on the actual CGWindowListCreateImageFromArray problem. Here's the working solution for that:
let windows: [CGWindowID] = [CGWindowID(17), CGWindowID(50), CGWindowID(59)]
let pointer: UnsafeMutablePointer<UnsafePointer<Void>> = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(windows.count)
for var i: Int = 0, n = windows.count; i < n; i++ {
pointer[i] = UnsafePointer<Void>(bitPattern: UInt(windows[i]))
}
let array: CFArray = CFArrayCreate(kCFAllocatorDefault, pointer, windows.count, nil)
let capture: CGImage = CGWindowListCreateImageFromArray(CGRectInfinite, array, CGWindowImageOption.Default)!
let image: NSImage = NSImage(CGImage: capture, size: NSZeroSize)
Swift.print(image) // <NSImage 0x7f83a3d16920 Size={1440, 900} Reps=("<NSCGImageSnapshotRep:0x7f83a3d2dea0 cgImage=<CGImage 0x7f83a3d16840>>")>
I'm not great at ObjC, please correct if wrong, but from what I understand by playing with the SonOfGrab example and particular piece of code below is that the final pointer structure contains window ids (UInt32) not inside the memory cell (memory property of UnsafePointer instance), but inside memory address (hashValue property).
const void *windowIDs[2];
windowIDs[0] = 10;
windowIDs[1] = 20;
It's interesting, since values aren't stored in the memory, but inside the address descriptors, with oldest architectures being 32-bit UInt32 values fit perfectly into address pointers. Perhaps back in the days when the memory was a limiting factor this made a lot of sense and was a great approach. Discovering this all night in Swift in 2016 made me suicidal.
What's worse it fails in Xcode 7.2 playground with certain window ids, probably because of the way it handles memory, but works in the actual app.

how to use AXUIElementCopyAttributeValue from Swift?

I am trying to use AXUIElementCopyAttributeValue from Swift. My best stab currently looks like this:
private func mainWindow() -> Optional<AXUIElementRef> {
let appRef = AXUIElementCreateApplication(self.pid())
var ptr: Unmanaged<AXUIElementRef>? = nil
var frontWindow: AXUIElementRef? = nil
let err = AXUIElementCopyAttributeValue(appRef, kAXMainWindowAttribute, &ptr)
if err == AXError(kAXErrorSuccess) {
frontWindow = ptr!.takeRetainedValue()
}
return frontWindow
}
Unfortunately kAXMainWindowAttribute is not in scope. This works in ObjC of course, but I can't figure out where the value is hiding when accessed from Swift. This isn't the first time I've had this problem, either, although previously I've been able to stumble around a bit and find it.
Also, I'd be happy to receive any stylistic suggestions here. I'm not convinced I'm doing this in the most natural way for Swift.
It is an old question but I still drop it here in case someone else searches for it:
let appRef = AXUIElementCreateApplication(pid)
var value: AnyObject?
AXUIElementCopyAttributeValue(appRef, kAXMainWindowAttribute as CFString, &value)
also, I assume you get the "Swift dynamic cast failed" error message because you try to cast from AXUIElement to AnyObject. You can cast it like so:
print(value as! AXUIElement)
This is not an answer yet, but perhaps might help someone get me an answer... I changed the code to this, in order to make forward progress:
func mainWindow() -> WBWindow? {
var result: WBWindow? = nil
var ptr: Unmanaged<AnyObject>?
let kAXMainWindow: CFString! = "AXMainWindow" as NSString
let appRef: AXUIElement! = AXUIElementCreateApplication(self.pid()).takeRetainedValue()
let err = AXUIElementCopyAttributeValue(appRef, kAXMainWindow, &ptr)
if err == AXError(kAXErrorSuccess) {
let val: AnyObject? = ptr?.takeRetainedValue()
if val != nil {
let value: AnyObject = val!
let description = CFCopyTypeIDDescription(CFGetTypeID(value))
println("type = \(description)")
}
else {
NSLog("got nil result")
}
// result = WBWindow(element: ptr?.takeRetainedValue() as? AXUIElement)
}
return result
}
This now builds -- yay! But, when running, the commented out line drops me into a stack:
0x1001b57ea: leaq 0x3f1a1(%rip), %rax ; "Swift dynamic cast failed"
I added the debugging code above and that produces:
type = AXUIElement
Sooooo. It's an AXUIElement that won't cast to one? Clearly I'm missing something obvious...

Specman - how to perform sub-type multi-extend, also for different kinds of sub-types

I want to achieve the following functionality:
extend RED GREEN BLUE packet {...}
this line will cause the struct members in the curly brackets,
to be added to all the specified subtypes of a certain enumerated type.
the result will look like this:
extend RED packet {...}
extend BLUE packet {...}
extend GREEN packet {...}
extend BIG MEDIUM RED BLUE GREEN packet {...}
this line will extend all possible combinations of the
items from each enumerated type with the struct members
that appear in the curly brackets.
the result will look like this:
extend MEDIUM RED packet {...}
extend MEDIUM BLUE packet {...}
extend MEDIUM GREEN packet {...}
extend BIG RED packet {...}
extend BIG BLUE packet {...}
extend BIG GREEN packet {...}
Thanks,
This macro solves this problem, yet there is a small limitation.
Since this macro is ‘define as computed’ , the struct you are going to apply it on should be defined in a different file than the file that uses the macro.
A simple use case is shown here: (suppose this macro is in a file called dac.e):
define <multi_when'statement> "ext_s \[<detr'name>,...\] <base'type> \{<sm'exp>,...\}" as computed {
var our_struct:rf_struct=rf_manager.get_type_by_name(<base'type>).as_a(rf_struct);
var fields:list of rf_field = our_struct.as_a(rf_struct).get_declared_fields();
var rf_type_list:list of rf_type;
var list_of_0_index:list of uint;
var field_names:list of string;
var list_of_enums:list of rf_enum;
var temp_index:uint=0;
var used_types_list:list of rf_type;
var enumerations:list of string;
var indices:list of uint;
var num_of_each_enum:list of uint;
var size_of_final_list_of_enumerations_to_be_used:uint=1;
var enum_items_list:list of rf_enum_item;
var final_list_of_enumerations_to_be_used: list of string;
var multiplication_list_algrtm1:list of uint;
var multiplication_list_algrtm2:list of uint;
var multiplication_uint_algrtm:uint=1;
if (<detr'names>.is_empty()){
error("you did not supply any when subtypes");
};
for each (field) in fields{
rf_type_list.add(field.get_type());
field_names.add(field.get_name());
};
for each (typ) in rf_type_list{
if (rf_type_list[index] is not a rf_enum){
rf_type_list.delete(index);
field_names.delete(index);
};
};
if (rf_type_list.is_empty()){
error("the type ",<base'type>," does not have any enumerated type fields.");
};
for each (typ) using index (typ_index) in rf_type_list {
num_of_each_enum.add(0);
if(indices.is_empty()){
indices.add(0);
}else {
indices.add(indices[typ_index-1])
};
enum_items_list = typ.as_a(rf_enum).get_items();
for each (enum_item) in <detr'names> {
if (enum_items_list.has(it.get_name()==enum_item)){
out(enum_item, " is found in ",typ.get_name());
enumerations.add(append(enum_item,"'",field_names[typ_index]));
indices[typ_index]+=1;
num_of_each_enum[typ_index]+=1;
};
};
};
for each in num_of_each_enum do { // avoid enums that are not used - to
if (it==0){
list_of_0_index.add(index);
};
};
if (!list_of_0_index.is_empty()){
list_of_0_index=list_of_0_index.reverse();
for each in list_of_0_index {
num_of_each_enum.delete(it);
indices.delete(it);
field_names.delete(it);
}
};
enumerations=enumerations.unique(it);
if (enumerations.is_empty()){
error("no legal enumerated values were used in the ext_s macro, please check that the arguments in square brackets are in the form of [<enum_item1>,<enum_item2>,...]");
};
//remove the last index (not relevant - and add 0 in the indices[0]
indices.add0(0);
indices.delete(indices.size()-1);
for each in num_of_each_enum do {
size_of_final_list_of_enumerations_to_be_used*=it;
};
for each in num_of_each_enum do {
multiplication_uint_algrtm*=it;
multiplication_list_algrtm1.add(size_of_final_list_of_enumerations_to_be_used/multiplication_uint_algrtm);
multiplication_list_algrtm2.add(size_of_final_list_of_enumerations_to_be_used/multiplication_list_algrtm1[index]);
};
//build the final list of string to be used in the extend statement:
for i from 1 to size_of_final_list_of_enumerations_to_be_used{
final_list_of_enumerations_to_be_used.add("");
};
for k from 0 to indices.size()-1 do {
temp_index=0;
for j from 0 to multiplication_list_algrtm2[k]-1 do {
for i from 0 to multiplication_list_algrtm1[k]-1 do {
final_list_of_enumerations_to_be_used[temp_index]=append(final_list_of_enumerations_to_be_used[temp_index]," ",enumerations[indices[k]+j%num_of_each_enum[k]]);
temp_index+=1;
};
};
};
for each in final_list_of_enumerations_to_be_used do {
result = appendf("%s extend %s %s {",result,it, <base'type> );
for each in <sm'exps> do {
result= appendf("%s %s;",result,it);
};
result = append(result , "};");
};
print result;
};
Note that this macro solves an interesting problem:
Suppose you have a list of a bunch of items of certain types (for example : {a1,a2,b1,b2,c1,c2,c3…}),
And you do not preliminarily know how many types are there in this list (in this example there are 3 types-a,b,c - but there could be more or less). So question is, how do you create a list of all possible combinations of all items from all type (for example: 0. a1-b1-c1 1.a1-b1-c2…..11.a2-b2-c3), without knowing how many types are there in the list? You can follow the code and figure out the algorithm to do that (using list of indices, how many items are there from each type and so….).
The file that should be loaded prior to macro (dac.e) is :
Struct.e:
<'
type t1:[A1,A2,A3,A4,A5];
type t2:[B1,B2,B3];
type t3:[C1,C2,C3];
struct s{
a:uint(bits:4);
t_1:t1;
t_2:t2;
t_3:t3;
};
'>
And the test file is :
<'
Import dac.e;
import struct.e;
//use the macro
ext_s [A1,A2,B1,B2] s {x:int,keep x==6,y:int,keep y==10};
extend sys{
s1:A1 B1 s;
s2:A2 B1 s;
s3:A1 s;
run() is also{
print s1;
print s2;
print s3;
};
};
'>
Please comment if you have any question.