Setting up Game Center using Swift? [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm quite new to Swift and iOS programming, and I was wondering how I would set up a Game Center leaderboard, add players' scores to the leaderboard and update players' scores using Swift.

If you Google how to set up Game Center, you should find some tutorials that will help you, they are a little out of date with iOS 8 so below are the bits that aren't so easy to translate over.
Authenticating a player
let lp = GKLocalPlayer.localPlayer()
if lp.authenticated == false {
lp.authenticateHandler() { (vc, error) -> Void in
println(error)
}
}
Add score to leader board
leaderboardName = "My first game"
let scoreObj = GKScore(leaderboardIdentifier: leadeboardName)
scoreObj.context = 0
scoreObj.value = score
GKScore.reportScores([scoreObj], withCompletionHandler: {(error) -> Void in
let alert = UIAlertView(title: "Success",
message: "Score updated",
delegate: self,
cancelButtonTitle: "Ok")
alert.show()
})

Related

Which way is recommended to use guard let? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to check preconditions for proceeding a function in iOS/Swift.
Option 1:
guard let name = str["name"], let age = str["age"] else {
print("name/age missing")
return
}
Option 2:
guard let name = str["name"] else {
print("name missing")
return
}
guard let age = str["age"] else {
print("age missing")
return
}
Which option is recommended.
This is completely unrelated to Swift.
From a UI / UX perspective certainly the 2nd option since you can now point to the exact input field that is missing.

convert from Swift to Objective C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert this Swift code to Objective C. I searched and did not find a way to convert automatically . Is there any way that can help convert ?
let token="0000000009"
SkyIdConfig.shared.setup(With: token)
var skyIdView:SkyDocumentsAnalyzer?
SkyIdConfig.shared.loadAndConfigure(){[unowned self] isConfigured,error in
if error != nil || !isConfigured {
print(error?.localizedDescription ?? "Error !!!!")
return
}
DispatchQueue.main.async {[unowned self] in
self.skyIdView=SkyIdBuilder.shared.getDocumentAnalyzerInstance(with: "03", lang: "fra")
if self.skyIdView != nil
{
self.skyIdView!.delegate=self
self.skyIdView?.modalPresentationStyle = .fullScreen
self.present(self.skyIdView!, animated: true, completion: nil)
}else{
print("Error in config loading")
}
}
}
Of course there is a way to convert ..
..manually. Which is the best way to understand whats going on.
your token is probably an
NSString *token = #"0000000009";
depending on the Class definition you have to look up how SkyIdConfig is done..
//assuming its something like..
[[SkyIdConfig shared] setupWith:token];
SkyDocumentsAnalyzer *skyIdView = [[SkyDocumentsAnalyzer alloc] init];
[[SkyIdConfig shared] loadAndConfigure];
before going forward in code read more about dispatching in objective-c (aka c) here...

onclick event popup in mapkit xcode 8 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i have a project done with maps using Open Street Map in xcode mapkit i already added there map and created a polyline, polygon, markers with popups.
i have remaining two parts to do one is to add zoom buttons to map
second one is i need to add a onclick event when i touch somewhere in the map place on the map i need to show a
popup event showing "hey u touch this lat,long".
i used several ways doing researches but i could not archive my goal
First you need add a UITapGestureRecognizer to your MKMapView in your viewDidLoad method
like this
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.showPopup(tapGesture:)))
self.mapView.addGestureRecognizer(tapGestureRecognizer)
after that you need to implement the Tap selector method, the main issue you will found is get the real location based on the touch screen coordinates your can do that using the MKMapView convert method
like this
let coordinate = self.mapView.convert(tapGesture.location(in: self.mapView), toCoordinateFrom: self.mapView) //here we convert touch location from Screen location to map coordinates
Your full code for show an alert popup with coordinates touched
func showPopup(tapGesture:UITapGestureRecognizer)
{
let coordinate = self.mapView.convert(tapGesture.location(in: self.mapView), toCoordinateFrom: self.mapView)
let alertViewController = UIAlertController(title: "TOUCH IN MAP", message: "coordinate is lat: \(coordinate.latitude) long: \(coordinate.longitude)", preferredStyle: .alert)
let action = UIAlertAction(title: "Accept", style: .destructive, handler: nil)
alertViewController.addAction(action)
self.present(alertViewController, animated: true, completion: nil)
}

How can I loop through objects in Swift? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following object:
(rates)
{
EUR: "1"
RON: 4.5
USD: 1.3
.
.
.
n: INT/STRING
}
Is there any function that does this?
if you care about the index, you can use a 'traditional' for loop - although #Eric points out this will soon be completely removed
for var i = 0; i < rates.count; i++
{
let rate = rates[i]
// do stuff with rate
}
The enumerate approach looks like this
for (index, rate) in rates.enumerate()
{
print("Do stuff with \(rate) at position \(index)")
}
if you just need each object in turn it's a bit easier
for rate in rates
{
// do stuff with rate
}
Because it's a dictionary, no enumerate() required:
var aDictionary: [String: Float] = ["EUR": 1, "RON": 4.5,
"USD": 1.3]
for (index,item) in aDictionary{
print(index,item)
}

Validate a textfield (regex) in swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I would like to validate an email textfield, to make sure that the only email it accepts ends only by (.edu) (ex: example#uwf.edu or example.students#uwf.edu). Anything else like (.com)(.co)(.net) or so are not acceptable.
Try this (Swift 2.0):
func endsWithEdu(str : String) -> Bool {
let regex = try! NSRegularExpression(pattern: "\\.edu$", options: [.CaseInsensitive])
return regex.numberOfMatchesInString(str, options: [], range: NSMakeRange(0, str.characters.count)) > 0
}
print(endsWithEdu("john#university.edu")) // true
print(endsWithEdu("john#university.com")) // false

Resources