KVC方法快速為資料對象賦值,kvc資料對象賦值

來源:互聯網
上載者:User

KVC方法快速為資料對象賦值,kvc資料對象賦值
利用KVC方法快速為資料對象賦值,如下例

在.h檔案中聲明如下形式方法:


- (id)initWithDic:(NSDictionary *)dic;

在對應.m檔案中

- (id)initWithDic:(NSDictionary *)dic

{   

     self = [superinit];   

     if (self)

{       

          [selfsetValuesForKeysWithDictionary:dic];   

}   

      returnself;

}


同時實現下面方法 ,實現內容一般不寫(必須實現,否則無法使用KVC)
- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{   

}


Java中怎在for迴圈語句中對 類對象數組 進行賦值

比如有一個User類,他的類對象是user,這個類對象數組是
users [] 他的長度為10;
那麼你可以這樣對這個users通過對數組的遍曆,可用用for,也可以用foreach來做。
用for做就是:
for ( int i =0;i<users.length;i++){
users[i] ="這裡就是你要加的東西"+i; //這裡就是你想要加的任何東西
}
這樣就把這個類對象數組賦值完了
 
java中怎從檔案裡讀取資料,然後賦值給對象數組

大體的實現功能,代碼寫的並不嚴謹,很多需要判斷的地方都沒判斷,你湊合看看功能是如何?的就行,至於各種判斷驗證什麼的你自己搞定吧。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileToArray {
public static void main(String[] args) throws IOException {
String path = FileToArray.class.getResource("data.txt").toString()
.substring(6);
File file = new File(path);
BufferedReader in = new BufferedReader(new FileReader(file));

List<Student0> students = new ArrayList<Student0>();
String line;
int flg = 1;
while ((line = in.readLine()) != null) {
String[] data = line.split(" ");
String name = data[0];
long id = Long.parseLong(data[1]);
String[] result = new String[data.length - 2];
for (int i=2; i<data.length; i++) {
result[i-2] = data[i];
}
Student0 sdt = new Student0(name, id, result);
students.add(sdt);
}

in.close();
in = null;
file = null;

for (Student0 std : students) {
System.out.println(std.getName() + " " + std.getId());
for (String res : std.getResult()) {
System.out.println(res);
}
System.out.println("=================");
}
}
}

class Student0 {
private String name;
private long id;
private String[] result;

public Student0(String name, long id, String[] result) {
this.name = name;
this.id = id;
this.result = result;
}

public String getName() {
return name;
}

......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.