我有这个简单的课程
btnPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("data",tvmername.getText().toString());
startActivity(intent);
}
});
/*---------------------*/
public class MovieAdapter extends ArrayAdapter {
private List<CarrotModel> carrotModelList;
private int resource;
private LayoutInflater inflater;
public MovieAdapter(Context context, int resource, List<CarrotModel> objects) {
super(context, resource, objects);
carrotModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(resource, null);
}
ImageView ivIcon;
TextView tvDiscount;
TextView tvDealDesc;
TextView tvOfftoDis;
TextView tvmeradd;
ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
tvmername = (TextView) convertView.findViewById(R.id.tvMerName);
tvDealDesc = (TextView) convertView.findViewById(R.id.tvDealDesc);
tvOfftoDis = (TextView) convertView.findViewById(R.id.tvOftoDis);
tvmeradd = (TextView) convertView.findViewById(R.id.tvmeradd);
ImageLoader.getInstance().displayImage(carrotModelList.get(position).getImageURL(), ivIcon);
tvDiscount.setText(carrotModelList.get(position).getDiscount() + "% OFF");
tvmername.setText(carrotModelList.get(position).getMerchantName());
tvDealDesc.setText(carrotModelList.get(position).getDealDescription());
tvOfftoDis.setText(carrotModelList.get(position).getOffersToDisplay());
tvmeradd.setText(carrotModelList.get(position).getMerchantAddress());
return convertView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
new JSONTask().execute("https://api.appery.io/rest/1/code/39aea091-00d8-4e02-af39-93d63f965d08/exec?ConponFor=normal");
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
new JSONTask().execute("https://api.appery.io/rest/1/code/39aea091-00d8-4e02-af39-93d63f965d08/exec?ConponFor=normal");
return "Deals";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
保存代码:
import UIKit
class SimpleModel: NSObject, NSCoding {
var name : String!
var done : Bool!
init(name:String) {
self.name = name
self.done = false
}
internal required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.done = aDecoder.decodeBool(forKey: "done") // BUG HERE
}
func encode(with encoder: NSCoder) {
encoder.encode(self.name, forKey: "name")
encoder.encode(self.done, forKey: "done")
}
}
读取代码:
let data = NSKeyedArchiver.archivedData(withRootObject: storageArray)
UserDefaults.standard.set(data, forKey: "storage")
UserDefaults.standard.synchronize()
当NSKeyedUnarchiver完成它的工作时会出现问题。我无法理解问题的来源。
谢谢!
答案 0 :(得分:12)
诀窍就是删除!形成原始类型。 如果你把!你说“制作一个隐式解包的可选项”,因此编码器将存档为NSNumber而不是Bool(或Int,Double)。 如果你删除!编码器将存档为Bool,事情按预期工作(我花了一个“事件”,这个解决方案由Apple提供)
答案 1 :(得分:12)
Bool
和Int
有新方法:
self.x = Int(decoder.decodeCInt(forKey: "Speed"))
self.y = decoder.decodeObject(forKey: "Serial") as! String
self.z = Bool(decoder.decodeBool(forKey: "Direction") )
答案 2 :(得分:7)
我遇到了同样的问题。 试试这个:
self.done = aDecoder.decodeObject(forKey: "done") as? Bool ?? aDecoder.decodeBool(forKey: "done")