(defun tictactoe3d ()
'(
((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
))
I need a function that will add a X or a O in the place of a NIL and I need that function to ask the user where he wants to put it. The board game is a tic tac toe 3D (3 boards intead of 1, 27 positions instead of 9). The first line is level 1 (and it is basically the same as having one tic tac toe with 9 positions). How can I add an element to a list like this. I know I have to verify if a position is nil.
The list you return is a constant. You must not mutate constants because it has undefined consequences. If you want to use purely functional data-structures, you can define operations that return modified copies of your board.
You are using a list where I think you should really be using an array: after all, a board is really a fixed-size grid. Here is how you allocate it:
(make-array '(3 3 3) :initial-element nil)
=> #3A(((nil nil nil) (nil nil nil) (nil nil nil))
((nil nil nil) (nil nil nil) (nil nil nil))
((nil nil nil) (nil nil nil) (nil nil nil)))
Then, you use aref and (setf aref):
(setf (aref board z y x) value)
Related
Cant seem to find an answer to this question.
I want to assign the below "UIlabel" and "UIimageview" nil, how would i do this in the same line?
productimage.image = nil
producttext.text = nil
I've tried the following which doesn't seem to work:
productimage.image = producttext.text = nil // this doesnt work
productimage.image = nil, producttext.text = nil // this doesnt work either
Appreciate any help.
You can use semicolon.
productimage.image = nil; producttext.text = nil
Hi I have one VC with two Container View (Mainview , sidemenuView) a set delegate in loginView to change value of MainView and sidemenuView , in main view everything work , but in sidemenuView when I want change label.text error Thread 1:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
how can I change two value with one protocols
if isUserLoginDelegate != nil {
let vc = sidemenu()
vc.isUserLogin(userInformation: jsonLogin , islogin: true)
isUserLoginDelegate?.isUserLogin(userInformation: jsonLogin , islogin: true)
self.navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
I have the following Dictionary:
["oct": nil,
"jan": Optional(3666.0),
"nov": nil,
"apr": nil,
"sep": nil,
"feb": nil,
"jul": nil,
"mar": nil,
"dec": nil,
"may": nil,
"jun": nil,
"aug": nil]
I want to enable a button only when any value of any key is not nil. Is there any functional "magics" to do it without a traditional loop?
Use contains(where:) on the dictionary values:
// Enable button if at least one value is not nil:
button.isEnabled = dict.values.contains(where: { $0 != nil })
Or
// Enable button if no value is nil:
button.isEnabled = !dict.values.contains(where: { $0 == nil })
You've already been provided with similar solutions, but here you go:
dict.filter({$0.value == nil}).count != 0
You can use filter to check if any value is nil in a dictionary.
button.isEnabled = dict.filter { $1 == nil }.isEmpty
I recommend to conform to the standard dictionary definition that a nil value indicates no key and declare the dictionary non-optional ([String:Double]).
In this case the button will be enabled if all 12 keys are present. This is more efficient than filter or contains
button.isEnabled = dict.count == 12
In Objective-C:
if (!myImageView) {
NSLog(#"hdhd");
}
else {
//DO SOMETHING
}
But in Swift:
if (!myImageView) {
println("somethin")
}
else {
println("somethin")
}
This code is giving me the error:
Could not find an overload for '!' that accepts the supplied arguments'
myImageView is class variable UIImageView.
What should I do?
Usually, the best way to deal with checking variables for nil in Swift is going to be with the if let or if var syntax.
if let imageView = self.imageView {
// self.imageView is not nil
// we can access it through imageView
} else {
// self.imageView is nil
}
But for this to work (or for comparison against nil with either == nil or != nil), self.imageView must be an optional (implicitly unwrapped or otherwise).
Non-optionals can not be nil, and therefore the compiler will not let you compare them against nil. They'll never be nil.
So if if let imageView = self.imageView or self.imageView != nil or self.imageView == nil are giving you errors, it's almost certainly because self.imageView is not an optional.
If your variable is of type UIImageView then it cannot ever be nil.
However if you want your code to be equivalent to your Objective-C code, change the variable type to UIImageView? (an optional type) and replace:
if (!myImageView) {
with:
if (myImageView == nil) {
Test for myImageView != nil or myImageView.image != nil.
Help me with Optional hell in Swift. How to return count of array for key "R". self.jsonObj can be null
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return (self.jsonObj["R"]! as? NSArray)?.count;
}
Let's take this a step at a time.
self.jsonObj may be nil so you need to treat it as an Optional:
self.jsonObj?["R"]
This will either return 1) nil if self.jsonObj is nil or if "R" is not a valid key or if the value associated with "R" is nil 2) an Optional wrapped object of some type. In other words, you have an Optional of some unknown type.
The next step is to find out if it is an NSArray:
(self.jsonObj?["R"] as? NSArray)
This will return an Optional of type NSArray? which could be nil for the above reasons or nil because the object in self.jsonObj was some other object type.
So now that you have an Optional NSArray, unwrap it if you can and call count:
(self.jsonObj?["R"] as? NSArray)?.count
This will call count if there is an NSArray or return nil for the above reasons. In other words, now you have an Int?
Finally you can use the nil coalescing operator to return the value or zero if you have nil at this point:
(self.jsonObj?["R"] as? NSArray)?.count ?? 0
I'm guessing that you'll want to return 0 if there's nothing in the array. In that case, try the Nil Coalescing Operator:
return (self.jsonObj?["R"] as? NSArray)?.count ?? 0;
Edit: As #vacawama's answer points out, it should be self.jsonObj?["R"] instead of self.jsonObj["R"]! in case self.jsonObj is nil.
assuming self.jsonObj is NSDictionary? or Dictionary<String, AnyObject>?
return self.jsonObj?["R"]?.count ?? 0