Swift 的 Playground 学习
我们一开始知道 Playground,也知道然后查看一些变量及变量的历史记录。偶尔还知道可以做视图甚至动画展示但是,现在,你特么看到的是 MarkDown 语法,有没有。
参考文献
使用优势
- 快速学习swift
- 快速测试代码效果
- 验证API
- 富文本注释
如下所有案例使用Xcode7下加载playground文件,可预览效果
文件下载链接
变量
至上而下执行,显示变量的当前值及历史记录的变化。
1 | var name = "杨志" |
UI视图
简单显示UI的基础元素
1 | let redView = UIView(frame:CGRectMake(0,0,100,100)) |
重头戏
动画展示 & 网络请求
这里需要对 XCPlayground 有一点了解,好像还对SpriteKit做了支持文章连接
1 | // 这个库需要import |
结构介绍
sources
我们直接在 Playground 上面写代码,然后编译器会实时编译我们代码,并将结果显示出来。但是效率很低,source的作用就可以发挥出来
resources
可以作为sandbox使用
1 | // 直接使用PicButton 这个class. |
Android APP开发设计模式 - 王胜
MVC(Standard Android)
目录结构
Weather
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28public class Weather {
private String date;//日期
private String textDay;//白天天气
private String textNight;//夜晚天气
private String high;//最高气温
private String low;//最低气温
public String getDisplay() {
StringBuilder sb = new StringBuilder();
sb.append(date);
sb.append("\t");
sb.append("白天:");
sb.append(textDay);
sb.append(", ");
sb.append("夜晚:");
sb.append(textNight);
sb.append(", ");
sb.append("最高气温:");
sb.append(high);
sb.append(", ");
sb.append("最低气温:");
sb.append(low);
return sb.toString();
}
// setter and getter
......
}
WeatherActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44public class WeatherActivity extends AppCompatActivity {
private ListView lv;
private LinearLayout llLoading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
lv = (ListView) findViewById(R.id.lv);
llLoading = (LinearLayout) findViewById(R.id.ll_loading);
loadData();
}
private void loadData() {
HttpEngine.get(Constants.API_WEATHER, new HttpEngine.JSONRequestCallback() {
@Override
public void onFailed(Call call, Exception e) {
if (call.request().url().url().toString().equals(Constants.API_WEATHER)) {
Log.e(WeatherActivity.class.getSimpleName(), e.getMessage());
Toast.makeText(WeatherActivity.this, "Get data failed!", Toast.LENGTH_LONG).show();
llLoading.setVisibility(View.GONE);
}
}
@Override
public void onSuccess(Call call, JSONObject jsonResult) {
if (call.request().url().url().toString().equals(Constants.API_WEATHER)) {
try {
JSONArray jsonArray = jsonResult.getJSONArray("results").getJSONObject(0).getJSONArray("daily");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
final List<Weather> data = gson.fromJson(jsonArray.toString(), new TypeToken<List<Weather>>(){}.getType());
WeatherAdapter adapter = new WeatherAdapter(WeatherActivity.this, data);
lv.setAdapter(adapter);
llLoading.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
} catch (JSONException e) {
Log.e(WeatherActivity.class.getSimpleName(), e.getMessage());
Toast.makeText(WeatherActivity.this, "Get data failed!", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
MVP
目录结构
- Weather 同上
IWeatherView && WeatherActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44public interface IWeatherView {
/**
* 刷新天气列表
* @param data
*/
void refreshWeatherList(List<Weather> data);
/**
* 显示错误信息
* @param e
*/
void showErrorInfo(Exception e);
}
public class WeatherActivity extends AppCompatActivity implements IWeatherView{
private ListView lv;
private LinearLayout llLoading;
private WeatherPresenter mWeatherPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
lv = (ListView) findViewById(R.id.lv);
llLoading = (LinearLayout) findViewById(R.id.ll_loading);
mWeatherPresenter = new WeatherPresenter(this);
mWeatherPresenter.getWeatherList();
}
@Override
public void refreshWeatherList(List<Weather> data) {
WeatherAdapter adapter = new WeatherAdapter(WeatherActivity.this, data);
lv.setAdapter(adapter);
llLoading.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
}
@Override
public void showErrorInfo(Exception e) {
Log.e(WeatherActivity.class.getSimpleName(), e.getMessage());
Toast.makeText(WeatherActivity.this, "Get data failed!", Toast.LENGTH_LONG).show();
llLoading.setVisibility(View.GONE);
}
}WeatherPresenter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35public class WeatherPresenter {
private IWeatherView mWeatherView;
public WeatherPresenter(@NonNull IWeatherView view) {
this.mWeatherView = view;
}
/**
* 通过网络请求获取天气列表数据
*/
public void getWeatherList() {
HttpEngine.get(Constants.API_WEATHER, new HttpEngine.JSONRequestCallback() {
@Override
public void onFailed(Call call, Exception e) {
if (call.request().url().url().toString().equals(Constants.API_WEATHER)) {
mWeatherView.showErrorInfo(e);
}
}
@Override
public void onSuccess(Call call, JSONObject jsonResult) {
if (call.request().url().url().toString().equals(Constants.API_WEATHER)) {
try {
JSONArray jsonArray = jsonResult.getJSONArray("results").getJSONObject(0).getJSONArray("daily");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
List<Weather> data = gson.fromJson(jsonArray.toString(), new TypeToken<List<Weather>>(){}.getType());
mWeatherView.refreshWeatherList(data);
} catch (JSONException e) {
mWeatherView.showErrorInfo(e);
}
}
}
});
}
}