【问题标题】:I need to implement a function that can be used multiple times我需要实现一个可以多次使用的功能
【发布时间】:2017-03-03 07:08:05
【问题描述】:

我有一个 swift 函数,我想转换为目标 C,谁能帮我转换它?任何帮助将不胜感激

     func getdata(_ send: NSString)
    {

        let url=URL(string:send as String)
        do
        {
            let allContactsData = try Data(contentsOf: url!)
            let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
            print(allContacts)
            if let musicJob = allContacts["Attendance"] as? [Any], !musicJob.isEmpty
            {
                print(musicJob)
                dataArray = allContacts["Attendance"] as! NSArray
                print(dataArray)

            }
}
           { let send: String = String(format:"http://182.18.182.91/RaosMobileService/Service1.svc/GetStudentAttendance_ByMonth/%@/%@/1/%d/2017",loadedUserName,studentId,row+1)
            self.getdata(send as NSString)
}

如何将其转换为目标 C,请提供任何帮助。我需要使用这个 func getdata(_ send: NSString) 多次

【问题讨论】:

    标签: objective-c multithreading swift3 singleton


    【解决方案1】:
    Objective-c中的

    NSJSONSerialization

    尝试1:如果Json返回数组

     -(void)getdata:(NSString *)send
        {
            NSURL *url = [[NSURL alloc]initWithString:send];
            NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    
            NSError *e = nil;
            NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
    
            if (!jsonArray) {
                NSLog(@"Error parsing JSON: %@", e);
            } else {
                for(NSDictionary *item in jsonArray) {
                    NSLog(@"Item: %@", item);
                }
            }
        }
    

    尝试 2:如果 Json 返回字典

    -(void)getdata:(NSString *)send
    {
        NSURL *url = [[NSURL alloc]initWithString:send];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    
        NSDictionary *res     = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",res);
     }
    

    调用方法:

      NSString *send = [NSString stringWithFormat:@"http://182.18.182.91/RaosMobileService/Service1.svc/GetStudentAttendance_ByMonth/%@/%@/1/%d/2017"];
        [self getdata:send];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2019-10-27
    相关资源
    最近更新 更多