我有一个相当长的时间,如果链在地图中寻找键并设置特定值,如果找到。我选择使用最少代码执行此操作的方式是这样的( 05-27 05:57:34.482: D/BONUSPACK(8392): GeocoderNominatim::getFromLocationName:http://nominatim.openstreetmap.org/search?format=json&accept-language=en&addressdetails=1&limit=1&q=Bolivia
05-27 05:57:34.512: W/System.err(8392): android.os.NetworkOnMainThreadException
05-27 05:57:34.512: W/System.err(8392): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-27 05:57:34.523: W/System.err(8392): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-27 05:57:34.523: W/System.err(8392): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-27 05:57:34.523: W/System.err(8392): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-27 05:57:34.523: W/System.err(8392): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-27 05:57:34.523: W/System.err(8392): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-27 05:57:34.523: W/System.err(8392): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-27 05:57:34.533: W/System.err(8392): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-27 05:57:34.533: W/System.err(8392): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-27 05:57:34.533: W/System.err(8392): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-27 05:57:34.533: W/System.err(8392): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-27 05:57:34.533: W/System.err(8392): at org.osmdroid.bonuspack.utils.HttpConnection.doGet(HttpConnection.java:73)
05-27 05:57:34.543: W/System.err(8392): at org.osmdroid.bonuspack.utils.BonusPackHelper.requestStringFromUrl(BonusPackHelper.java:71)
05-27 05:57:34.543: W/System.err(8392): at org.osmdroid.bonuspack.location.GeocoderNominatim.getFromLocationName(GeocoderNominatim.java:225)
05-27 05:57:34.543: W/System.err(8392): at org.osmdroid.bonuspack.location.GeocoderNominatim.getFromLocationName(GeocoderNominatim.java:271)
05-27 05:57:34.543: W/System.err(8392): at com.example.GeocoderNominatim.MainActivity$MapFragment.onCreateView(MainActivity.java:340)
05-27 05:57:34.543: W/System.err(8392): at android.app.Fragment.performCreateView(Fragment.java:1700)
05-27 05:57:34.543: W/System.err(8392): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-27 05:57:34.553: W/System.err(8392): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-27 05:57:34.553: W/System.err(8392): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-27 05:57:34.553: W/System.err(8392): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-27 05:57:34.553: W/System.err(8392): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-27 05:57:34.553: W/System.err(8392): at android.os.Handler.handleCallback(Handler.java:733)
05-27 05:57:34.553: W/System.err(8392): at android.os.Handler.dispatchMessage(Handler.java:95)
05-27 05:57:34.563: W/System.err(8392): at android.os.Looper.loop(Looper.java:136)
05-27 05:57:34.573: W/System.err(8392): at android.app.ActivityThread.main(ActivityThread.java:5146)
05-27 05:57:34.573: W/System.err(8392): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 05:57:34.573: W/System.err(8392): at java.lang.reflect.Method.invoke(Method.java:515)
05-27 05:57:34.573: W/System.err(8392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
05-27 05:57:34.573: W/System.err(8392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
05-27 05:57:34.573: W/System.err(8392): at dalvik.system.NativeStart.main(Native Method)
是m
)
map[string]interface{}
这感觉有点笨重,我正在做所有这些var ok bool
var s interface{}
if s, ok = m["key1"]; ok {
} else if s, ok = m["key2"]; ok {
....
} else if s, ok = m["keyN"]; ok {
} else {
return RuhRohError
}
g.Id = s.(string)
来设置条件中的变量。这是否有惯用的方法?我认为这种做法并没有立即显示我想要做的事情。
答案 0 :(得分:2)
例如,
for feature in features as! [CIQRCodeFeature]
输出:
package main
import "fmt"
func findValue(m map[string]interface{}, keys []string) (interface{}, bool) {
for _, key := range keys {
if value, ok := m[key]; ok {
return value, true
}
}
return nil, false
}
func main() {
m := map[string]interface{}{"keyn": "valuen"}
keys := []string{"key1", "key2", "keyn"}
s, found := findValue(m, keys)
if !found {
return
}
id := s.(string)
fmt.Println(id)
}
答案 1 :(得分:1)
实际上我认为您提供的代码非常易于阅读和理解。它有点冗长,但没有魔法;)。因此,如果只有一个地方你必须编写这段代码,我会保留原样。如果你需要写几次,我会考虑编写另一条评论中提到的findValue
函数。