天道酬勤,学无止境

swift

使用“Result”调用完成处理函数(Call completion Handler function using "Result")

问题 如何在使用“Result”时调用 getEmployee 函数? struct Employee { let name: String let designation: String } func getEmployee(name: String, completion: @escaping(Result<Employee?, Error>) -> Void) { } 回答1 首先你需要让你的结构符合 Codable struct Employee: Codable { let name, designation: String } 如果解码成功与您的员工.success(employee)或者如果它失败并通过您的错误.failure(error)失败,那么您需要从您的服务器获取您的数据并调用完成: func getEmployee(name: String, completion: @escaping(Result<Employee, Error>) -> Void) { URLSession.shared.dataTask(with: URL(string: "http://www.example.com/getEmployee.api?name=\(name)")!) { data, response, error in guard let data = data

2021-10-27 03:51:47    分类:技术分享    swift   closures

Transition to UITabBarController

I need help with bug fix. I'm use segue to transit between ViewControllers. I have UITabBarController with one UIViewController. When I click on button in my vc I move to second vc. My second vc doesn't have TabBar. (use segue). In my second vc I have button and UIAlertController. When I click on button or alert I must transit to first vc with TabBar, but my TabBar doesn't displayed. How to fix this ? I don't need to use NavigationController. let alert = UIAlertController(title: "Поздравляю", message: "Тренировка окончена", preferredStyle: UIAlertController.Style.alert) alert.addAction

2021-10-27 03:51:14    分类:问答    swift   segue   uistoryboardsegue

populate image array with parse in swift

Hi I'm new with swift and parse.com, I'm trying do populate my array with already saved images in parse, try to use the dispatch_async but don't know how this works, heres the code: //imageArray declaration in table: var imageArray: Array<UIImage> = [] override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell var query = PFQuery(className: "ParseClass") query.findObjectsInBackgroundWithBlock { ( objects: [AnyObject]?, error: NSError?) -

2021-10-27 03:51:02    分类:问答    xcode   image   swift   parse-platform

How to set NSTimer limit for counting?

How do I set a NSTimer limit for counting? Firstly, to get a feel of my problem, here's a screenshot of my Xcode project - http://i.gyazo.com/178450cfdbf45a2508856cc79fd215ec.png Secondly, here's the code to understand what's going wrong - else if timerRunning==true && timerCount==timerMaximum { timer.invalidate() timerRunning = false timerCount=0 timerLabel.text="0 secs" intervalAlert() } I tried to do this and the code takes no effect on the execution of the program. The timer continues to count after the timerMaximum variable (which I made sure is global). I figured that this has something

2021-10-27 03:50:17    分类:问答    ios   swift

swift detailTextLabel not showing up

Ive checked around stackOverFlow for a solution but it seemed like it still didnt work for my code.. my detailTextLabel still does not show up :( Wondering what I am doing wrong, here's my code func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell() // display our ranked data cell.textLabel?.text = "\(championListInGame[indexPath.row])" if let url = NSURL(string: "http://ddragon.leagueoflegends.com/cdn/img/champion/loading/\(championListInGame[indexPath.row])_0.jpg"){ if let urlData = NSData(contentsOfURL: url){

2021-10-27 03:45:27    分类:问答    swift   colors   tableviewcell

xCode 7: how to fix initialization of variable was never used consider replacing with assignment to '_' or removing it

let alert = UIAlertController(title: "Alert", message: "isDone", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in self.dismissViewControllerAnimated(true, completion: nil) } the var alert and let okAction are error, the error message below "initialization of variable was never used consider replacing with assignment to '_' or removing it" I can't find the error, please help me to fix this error. Thank you!

2021-10-27 03:45:11    分类:问答    xcode   swift

Using Returned JSON Status in an if statement (Checking IAP Auto Renew Receipt )

Im want to check the current subscription status of an Auto Renew In App purchase. Im getting the receipt data via JSON, im after some advice on how to use/query the returned data if let parseJSON = json { println("Recipt \(parseJSON)") } Returns Recipt { environment = Sandbox; status = 21004; } Whilst I know this wouldn't compile, I want to to do is something along the lines of an if statement like: if parseJSON contains status = 21004 { //Do something }

2021-10-27 03:44:17    分类:问答    json   swift   in-app-purchase

Catch selector using class_addMethod in for swift classes

I've got ObjC class: @interface SomeClass : NSObject @property(nonatomic, retain) NSNumber *sample; @end and #import "SomeClass.h" @import ObjectiveC; @implementation SomeClass @dynamic sample; + (BOOL)resolveInstanceMethod:(SEL)aSEL { NSLog(@"resolveInstanceMethod"); if ([NSStringFromSelector(aSEL) hasPrefix:@"set"]) { class_addMethod([self class], aSEL, (IMP)setPropertyIMP, "v@:@"); } else { class_addMethod([self class], aSEL, (IMP)propertyIMP, "@@:"); } return YES; } static id propertyIMP(id self, SEL _cmd) { __block id value = nil; NSLog(@"propertyIMP"); return value; } static void

2021-10-27 03:41:24    分类:问答    objective-c   swift

关系实体的数据是(Relationship's entity's data is <fault>)

问题 我在此链接中询问了参考问题。我使用 NSMangedObjectID 得到了答案。现在我使用以下函数从其 componentID 中获取组件实体。在 SyncSurveyWebReadings 的 API 响应中,我只获取 componentId,我需要获取组件实体并设置作为关系。现在不存在时间问题。但可能是关系没有设置。当我打印我得到的关系对象时, Optional<Array<SurveyReadingWeb>> ▿ some : 1 element - 0 : <GazSurvey_Dev.SurveyReadingWeb: 0x282e0c8c0> (entity: SurveyReadingWeb; id: 0xf496ed8ab8c8dfd6 <x-coredata://3A27D5DB-BFBC-4005-BA69-59CB5C392617/SurveyReadingWeb/p4000> ; data: <fault>) func getComponentFromID(compID : Int32) -> Components? { var component :Components? = nil var objectId : NSManagedObjectID? = nil let fetchRequest = NSFetchRequest

2021-10-27 03:39:52    分类:技术分享    ios   swift   core-data   relationship   fault

How to change the datepicker current date text color?

Since using datepicker with a dark background color, I set its textColor to white. All the date text shows what I want except the current date. If today is 7/23/2015, its text will be black but not white. I tried and found no solution, anyone can help? Current date text color Not current date text color:

2021-10-27 03:38:44    分类:问答    ios   swift   colors   datepicker   uidatepicker