Returning an array of tuples - xcode

func timesForDepartureStation(departureStation: Station, #arrivalStation: Station) -> Array<(departureTime: NSDate, arrivalTime: NSDate)>{
For some reason, the Xcode LINT tool completely stop me from compiling this line of code. It notes that the # is Extraneous (even though it's not and I just want the arrivalStation to be an external parameter as well), and when calling from an external class... it doesn't even recognize the second parameter. I have no clue why, but I've restarted Xcode numerous times and am very tempted to do a reinstall. Has anyone found a similar issue?

By default every parameter except the first uses the parameter name as the external name there is no need to force the second argument. So this func signature should give you what you want:
func timesForDepartureStation(departureStation: Station, arrivalStation: Station) -> Array<(departureTime: NSDate, arrivalTime: NSDate)>
Then you can call this function like this:
timesForDepartureStation(departStation, arrivalStation: arrStation)
If you want to force the first argument to use an external name you can use the # infront of departureStation, but that is not necessary based on the name of the function.
As well if you are looking to not use named parameters for a function you must put an _ in front of the parameter in the definition like so:
func timesForDepartureStation(departureStation: Station, _ arrivalStation: Station) -> Array<(departureTime: NSDate, arrivalTime: NSDate)>
Then it can be called like so:
timesForDepartureStation(departStation, arrStation)

Restarting my computer and Xcode fixed everything. Silly betas

Related

Where is 's' defined in https://github.com/Workiva/go-datastructures/tree/master/bitarray?

I do not see where s is defined. Guru will not tell me. All I get is "no object for identifier" but it knows about the k right beside it. Here is a snippet that is typical of the linked code:
func getIndexAndRemainder(k uint64) (uint64, uint64) {
return k / s, k % s
}
The one letter variable name definitely makes it harder to grep around for. I have looked for the usual suspects: var s uint64, s := ..., and nothing. Clearly it needs to be a global value defined somewhere.
This leaves me with two questions:
Where is s coming from?
How would I find it without asking here?
EDIT:
For others who stumble on this.
Guru failed me because I did not checkout the source for the package under a proper Go workspace by placing the git clone under /some/path/src and setting the GOPATH to /some/path. So while I thought GOPATH=. guru definition s would work, the GOPATH was ignored. guru could find k because it is in the file but it did not know how to look in other files.
My grep failed cause const uses a simple = not a :=. I will remember this when grepping in the future.
It is defined in go-datastructures/bitarray/block.go:
// s denotes the size of any element in the block array.
// For a block of uint64, s will be equal to 64
// For a block of uint32, s will be equal to 32
// and so on...
const s = uint64(unsafe.Sizeof(block(0)) * 8)
As the variable s was not defined in the function, and it was not prefixed by a package name or alias, it had to be a global (variable or constant) of the bitarray package.
Once that was known, I went through every file in the folder go-datastructures/bitarray that was not suffixed with _test and I looked for a top-level declaration for s.
It's defined in go-datastructures/bitarray/block.go, line #33:
const s = uint64(unsafe.Sizeof(block(0)) * 8)
"Modern" IDEs with Go support usually have the ability to go to the definition of a symbol / identifier your cursor is at or what you click on. For example in Atom with the Go-plus plugin you can go to the definition by holding down the CTRL key while clicking.
These IDEs use the godef open source tool to find the definition source file and line, you may also use it directly. You can find the godef documentation here: https://godoc.org/github.com/rogpeppe/godef
Another tool, guru is also capable of tracking the definition of it. Both guru and godef works in Atom, and were able to jump to block.go, to the definition of s. But it's much easier to use an "armored" IDE and just do a simple click.
Also note that the success of using grep and patterns is limited, as variable and constant declarations can be grouped, and the following are also valid declarations:
var (
longer = 3
s = uint64(3)
)
Or:
var (
s = someExpression
longer = 3
)
Or:
const (
p = uint64(iota)
s
x
)

Using termios in Swift

Now that we've reached Swift 2.0, I've decided to convert my, as yet unfinished, OS X app to Swift. Making progress but I've run into some issues with using termios and could use some clarification and advice.
The termios struct is treated as a struct in Swift, no surprise there, but what is surprising is that the array of control characters in the struct is now a tuple. I was expecting it to just be an array. As you might imagine it took me a while to figure this out. Working in a Playground if I do:
var settings:termios = termios()
print(settings)
then I get the correct details printed for the struct.
In Obj-C to set the control characters you would use, say,
cfmakeraw(&settings);
settings.c_cc[VMIN] = 1;
where VMIN is a #define equal to 16 in termios.h. In Swift I have to do
cfmakeraw(&settings)
settings.c_cc.16 = 1
which works, but is a bit more opaque. I would prefer to use something along the lines of
settings.c_cc.vim = 1
instead, but can't seem to find any documentation describing the Swift "version" of termios. Does anyone know if the tuple has pre-assigned names for it's elements, or if not, is there a way to assign names after the fact? Should I just create my own tuple with named elements and then assign it to settings.c_cc?
Interestingly, despite the fact that pre-processor directives are not supposed to work in Swift, if I do
print(VMIN)
print(VTIME)
then the correct values are printed and no compiler errors are produced. I'd be interested in any clarification or comments on that. Is it a bug?
The remaining issues have to do with further configuration of the termios.
The definition of cfsetspeed is given as
func cfsetspeed(_: UnsafeMutablePointer<termios>, _: speed_t) -> Int32
and speed_t is typedef'ed as an unsigned long. In Obj-C we'd do
cfsetspeed(&settings, B38400);
but since B38400 is a #define in termios.h we can no longer do that. Has Apple set up replacement global constants for things like this in Swift, and if so, can anyone tell me where they are documented. The alternative seems to be to just plug in the raw values and lose readability, or to create my own versions of the constants previously defined in termios.h. I'm happy to go that route if there isn't a better choice.
Let's start with your second problem, which is easier to solve.
B38400 is available in Swift, it just has the wrong type.
So you have to convert it explicitly:
var settings = termios()
cfsetspeed(&settings, speed_t(B38400))
Your first problem has no "nice" solution that I know of.
Fixed sized arrays are imported to Swift as tuples, and – as far as I know – you cannot address a tuple element with a variable.
However,Swift preserves the memory layout of structures imported from C, as
confirmed by Apple engineer Joe Groff:. Therefore you can take the address of the tuple and “rebind” it to a pointer to the element type:
var settings = termios()
withUnsafeMutablePointer(to: &settings.c_cc) { (tuplePtr) -> Void in
tuplePtr.withMemoryRebound(to: cc_t.self, capacity: MemoryLayout.size(ofValue: settings.c_cc)) {
$0[Int(VMIN)] = 1
}
}
(Code updated for Swift 4+.)

greenline Xcode EXC_BAD_INSTRUCTION on NSUserDefaults Optional

I've been writing an app that involves using NSUserDefaults to store a few Int variables and it's been working fine. I thought I was finished and was doing some final testing and one of the first lines of code that I wrote, and that has been working consistently before, has failed me.
Apparently the green line error is supposed to occur if I try to unwrap an optional that has a value of nil, but this variable is still very much an optional
var savedTotalSeconds: Int? = userDefaults.objectForKey("totalSecondsKey") as Int?
Why would this possibly return an error? It was working fine before and I only changed things I thought were unrelated to it. In the app I have a button to remove this stored value via:
userDefaults.removeObjectForKey("totalSecondsKey")
What could possibly have gone wrong?
Try using 'as? Int' instead of 'as Int?'
The difference is that the first one tries, and might fail, at casting to Int. That failure will be captured in the optionality of the resulting variable.
The second one tries to coerce the object to 'Int?'.

Is it possible to reference value of another watched expression in vs2010?

When I hit a breakpoint I have "this" in my watch window
this -> 0x2cceb42c
I copy that value into a new line in my watch window (it displays both the name and the value in hex)
0x2cceb42c -> 0x2cceb42c
On a third line I cast my value into a pointer to my class:
(MyClass*)0x2cceb42c -> { members of class... }
The problem is, next time I run the program the address has changed so I have to edit the address on my third line. Only, I'm not just using it in the third line, but also in 5 other watch expressions. Which means the next time I run the program I have to change the address in all 5 watched expressions.
What I would like to do is have my 5 watch expressions refer to the value in line 2 - then I only ever need to change the address in one place and all my watches will update automatically.
Is this possible? Or can anyone suggest a trick to achieve as close to this as possible?
Clarification: I want to see the results of my 5 watch expressions when I'm on a breakpoint elsewhere in the code (where "this" is no longer the value I'm interested in, which is why I'm copying the address out of "this").

Shadow problem when Getting a package from a Palette action

My simple first Palette is suppose to:
Append my packages Path to $Path
ActionMenu["test",{"The Simple Packages Path":> AppendTo[$Path, ToFileName[{NotebookDirectory[], "02 Simple Packages"}]]}]
Get my packages
ActionMenu["Load Packages", {"Get my package":> Get["myPackage`"]}]
Place on the selected input cell (or on a new input cell), a given input expression, containing different place holders.
OpenerView[{"my Package", Button["construct", Paste[StandardForm#Defer#construct[Placeholder["description"],Placeholder["another description"]]]]}]
The problem is that I keep getting "shadow" messages when I click on the "get my package" action menu item. And I'm sure I'm not double loading the package intentionally. When I click on "construct", it writes Global`construct["description","another description"]. But I'm sure I didn't define it before getting the package (I killed the kernel on my tests).
Do you know what is wrong?
(I use Get on my packages, instead of Needs, to ensure a clean start of the package context)
Also: do you know of a simpler way of doing the Paste[StandardForm#Defer... that ensures both that the expression being paste isn't evaluated and that it goes into an input cell, even when there's no cell selected?
Ok, it seems that your problem is due to the interplay between parsing and interface creation. What you would like in this case is to delay the parsing of package symbols in your interface - constructing code (package symbols that you use in button action functions), from the interface - creation time, until the "press button" time (assuming that by that time, the package has been loaded). Here is one way to do it:
Column[{ActionMenu["Load Packages",
{"Get my package" :> Get["ANOVA`"]}],
OpenerView[{"ANOVA", Button["construct",
With[{sym = Symbol["ANOVA"]},
Paste[StandardForm#Defer#sym[Placeholder["DATA"]]]]]}]}]
What we did here is to use With to inject the symbol into the code for the button function. But, at the time your interface code is parsed, we prevent the creation of the Global symbol with this name - this is what happens otherwise, and this is what causes your problem.
EDIT
If you know for sure that you only use symbols (functions) from packages, and not from the Global' context, here is a version that will be "protected" from this problem: it will Remove the generated symbol if its context turns out to be Global' - and thus pressing button before the package has been loaded would result merely in a warning message (I use the symbol package to attach the message to - should be replaced by whatever is the name of your interface - making function):
package::noload = "Please load the package containing symbol `1`";
Column[{ActionMenu["Load Packages",
{"Get my package" :> Get["ANOVA`"]}],
OpenerView[{"ANOVA", Button["construct",
With[{sym = Symbol["ANOVA"]},
If[Context[sym] === "Global`",
Message[package::noload, Style[ToString[sym], Red]];
Remove[sym];,
(* else *)
Paste[StandardForm#Defer#sym[Placeholder["DATA"]]]]]]}]}]
Well, I don't have your package, so for testing I changed the action menu to get the ANOVA package:
ActionMenu["Load Packages", {"Get my package" :> Get["ANOVA`"]}]
ANOVA[{{1, 1}, {1, 2}, {2, 4}, {2, 3}}] now works without a hitch. No complaints about shadowing. This suggests that the cause of your shadowing problem lies somewhere else. I noticed, though, that the word ANOVA stays blue. This will be related to the problem you reported here.

Resources